Skip to content

Commit a5c4031

Browse files
committed
feat: add motion to theme
1 parent 5ff487b commit a5c4031

File tree

5 files changed

+133
-6
lines changed

5 files changed

+133
-6
lines changed

.changeset/odd-impalas-invent.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@strapi/design-system': minor
3+
---
4+
5+
feat: add motion properties to theme as well as transitions

packages/design-system/src/styles/easings.ts renamed to packages/design-system/src/styles/motion.ts

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export const EASINGS = {
1+
import { keyframes } from 'styled-components';
2+
3+
const EASINGS = {
24
easeInSine: 'cubic-bezier(0.47, 0, 0.745, 0.715)',
35
easeOutSine: 'cubic-bezier(0.39, 0.575, 0.565, 1)',
46
easeInOutSine: 'cubic-bezier(0.39, 0.575, 0.565, 1)',
@@ -35,3 +37,86 @@ export const EASINGS = {
3537

3638
authenticMotion: 'cubic-bezier(.4,0,.2,1)',
3739
};
40+
41+
const TIMINGS = {
42+
'320': '320ms',
43+
'200': '200ms',
44+
'120': '120ms',
45+
};
46+
47+
/**
48+
* A collection of common transitions attached to the theme to be used.
49+
*/
50+
const TRANSITIONS = {
51+
color: `color ${TIMINGS['120']} ${EASINGS.easeOutQuad}`,
52+
backgroundColor: `background-color ${TIMINGS['120']} ${EASINGS.easeOutQuad}`,
53+
};
54+
55+
const ANIMATIONS = {
56+
appear: keyframes`
57+
from {
58+
opacity: 0;
59+
}
60+
to {
61+
opacity: 1;
62+
}
63+
`,
64+
slideDownIn: keyframes`
65+
from {
66+
opacity: 0;
67+
transform: translateY(-10px);
68+
}
69+
to {
70+
opacity: 1;
71+
transform: translateY(0);
72+
}
73+
`,
74+
slideDownOut: keyframes`
75+
from {
76+
opacity: 1;
77+
transform: translateY(0);
78+
}
79+
to {
80+
opacity: 0;
81+
transform: translateY(-10px);
82+
}
83+
`,
84+
slideUpIn: keyframes`
85+
from {
86+
opacity: 0;
87+
transform: translateY(10px);
88+
}
89+
to {
90+
opacity: 1;
91+
transform: translateY(0);
92+
}
93+
`,
94+
slideUpOut: keyframes`
95+
from {
96+
opacity: 1;
97+
transform: translateY(0);
98+
}
99+
to {
100+
opacity: 0;
101+
transform: translateY(10px);
102+
}
103+
`,
104+
fadeIn: keyframes`
105+
from {
106+
opacity: 0;
107+
}
108+
to {
109+
opacity: 1;
110+
}
111+
`,
112+
fadeOut: keyframes`
113+
from {
114+
opacity: 1;
115+
}
116+
to {
117+
opacity: 0;
118+
}
119+
`,
120+
};
121+
122+
export { ANIMATIONS, EASINGS, TRANSITIONS, TIMINGS };

packages/design-system/src/themes/__tests__/extendTheme.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ describe('extendTheme', () => {
3333
fontSizes: /* put the overrides for the fontSizes key */,
3434
lineHeights: /* put the overrides for the lineHeights key */,
3535
fontWeights: /* put the overrides for the fontWeights key */,
36-
easings: /* put the overrides for the easings key */,
36+
transitions: /* put the overrides for the transitions key */,
37+
motion: /* put the overrides for the motion key */,
3738
})
3839
"
3940
`);
@@ -68,7 +69,8 @@ describe('extendTheme', () => {
6869
fontSizes: /* put the overrides for the fontSizes key */,
6970
lineHeights: /* put the overrides for the lineHeights key */,
7071
fontWeights: /* put the overrides for the fontWeights key */,
71-
easings: /* put the overrides for the easings key */,
72+
transitions: /* put the overrides for the transitions key */,
73+
motion: /* put the overrides for the motion key */,
7274
})
7375
"
7476
`);

packages/design-system/src/themes/common-theme.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EASINGS } from '../styles/easings';
1+
import { EASINGS, TIMINGS, TRANSITIONS } from '../styles/motion';
22

33
import { Sizes, sizes } from './sizes';
44

@@ -26,7 +26,11 @@ export interface CommonTheme {
2626
semiBold: 500;
2727
bold: 600;
2828
};
29-
easings: typeof EASINGS;
29+
motion: {
30+
easings: typeof EASINGS;
31+
timings: typeof TIMINGS;
32+
};
33+
transitions: typeof TRANSITIONS;
3034
}
3135

3236
export const commonTheme: CommonTheme = {
@@ -53,5 +57,9 @@ export const commonTheme: CommonTheme = {
5357
semiBold: 500,
5458
bold: 600,
5559
},
56-
easings: EASINGS,
60+
transitions: TRANSITIONS,
61+
motion: {
62+
easings: EASINGS,
63+
timings: TIMINGS,
64+
},
5765
};

packages/design-system/src/utilities/DesignSystemProvider.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { useLayoutEffect } from 'react';
2+
13
import { Provider as TooltipProvider, TooltipProviderProps } from '@radix-ui/react-tooltip';
24
import { DefaultTheme, ThemeProvider } from 'styled-components';
35

@@ -40,6 +42,31 @@ const DesignSystemProvider = ({
4042
theme = lightTheme,
4143
tooltipConfig,
4244
}: DesignSystemProviderProps) => {
45+
useLayoutEffect(() => {
46+
/**
47+
* Switching themes should not trigger transitions and animations on elements.
48+
* The following code will remove all transitions and animations when the theme changes.
49+
*/
50+
const css = document.createElement('style');
51+
css.type = 'text/css';
52+
css.appendChild(
53+
document.createTextNode(`
54+
* {
55+
-webkit-transition: none !important;
56+
-moz-transition: none !important;
57+
-o-transition: none !important;
58+
-ms-transition: none !important;
59+
transition: none !important;
60+
animation: none !important;
61+
}
62+
`),
63+
);
64+
document.head.appendChild(css);
65+
66+
const _ = window.getComputedStyle(css).opacity;
67+
document.head.removeChild(css);
68+
}, [theme]);
69+
4370
return (
4471
<Provider locale={locale}>
4572
<ThemeProvider theme={theme}>

0 commit comments

Comments
 (0)