Skip to content

Commit 2a0e15f

Browse files
committed
chore: add migration guide for styled-components@6
1 parent fc1e69b commit 2a0e15f

File tree

5 files changed

+127
-149
lines changed

5 files changed

+127
-149
lines changed

docs/components/Typography.tsx

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,41 @@
1-
import { Typography, TypographyProps } from '@strapi/design-system';
1+
import { Typography, TypographyComponent, TypographyProps } from '@strapi/design-system';
22
import { styled, css } from 'styled-components';
33

44
const H1 = (props: TypographyProps) => (
5-
<Typography tag="h1" variant="alpha" textColor="neutral800" marginBottom="0.5em" {...props} />
5+
<Typography {...props} tag="h1" variant="alpha" textColor="neutral800" marginBottom="0.5em" />
66
);
77

88
const H2 = (props: TypographyProps) => (
9-
<Typography tag="h2" variant="beta" textColor="neutral800" marginBottom="1em" marginTop="2em" {...props} />
9+
<Typography {...props} tag="h2" variant="beta" textColor="neutral800" marginBottom="1em" marginTop="2em" />
1010
);
1111

1212
const H3 = (props: TypographyProps) => (
13-
<Typography tag="h3" variant="delta" textColor="neutral800" marginBottom="1em" marginTop="1.4em" {...props} />
13+
<Typography
14+
{...props}
15+
tag="h3"
16+
variant="delta"
17+
textColor="neutral800"
18+
marginBottom="1em"
19+
marginTop="1.4em"
20+
fontSize={4}
21+
/>
1422
);
1523

1624
const H4 = (props: TypographyProps) => (
1725
<Typography
26+
{...props}
1827
tag="h4"
1928
variant="epsilon"
2029
textColor="neutral800"
2130
marginBottom="1em"
2231
marginTop="1.4em"
23-
fontWeight="bold"
24-
{...props}
32+
fontWeight="semiBold"
2533
/>
2634
);
2735

28-
const P = (props: TypographyProps) => <Paragraph tag="p" variant="epsilon" textColor="neutral700" {...props} />;
36+
const P = (props: TypographyProps) => (
37+
<Paragraph {...props} tag="p" variant="epsilon" textColor="neutral700" fontSize={2} />
38+
);
2939

3040
const codeStyles = css`
3141
color: ${({ theme }) => theme.colors.neutral700};
@@ -34,7 +44,7 @@ const codeStyles = css`
3444
padding: 0.6rem;
3545
`;
3646

37-
const Paragraph = styled(Typography)`
47+
const Paragraph = styled<TypographyComponent<'p'>>(Typography)`
3848
& code {
3949
${codeStyles}
4050
}
File renamed without changes.

docs/stories/getting started/migration guides/migration-v1-v2.mdx

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@ import { Meta } from '@storybook/blocks';
66

77
## Design System
88

9+
### Peer dependency updates
10+
11+
The design-system & icons peer-deps have been updated, we removed `react-router-dom` and updated `styled-components`.
12+
13+
```diff
14+
// package.json
15+
{
16+
peerDependencies: {
17+
"react": "^18.0.0",
18+
"react-dom": "^18.0.0",
19+
- "react-router-dom": "^5.2.0",
20+
- "styled-components": "^5.3.0",
21+
+ "styled-components": "^6.0.0"
22+
}
23+
}
24+
```
25+
926
### Remove direct imports
1027

1128
Users can no longer `import { Combobox } from '@strapi/design-system/Combobox'`, instead they should import _all_ things
@@ -92,7 +109,29 @@ return (
92109
+ import { Toggle } from '@strapi/design-system'
93110
```
94111

95-
### Box, Flex & Typography are now all react components
112+
### Upgraded styled-components to V6
113+
114+
#### `as` is no longer a prop, use `tag` instead
115+
116+
A common issue we faced in Strapi was the behaviour of the `as` prop in conjunction with how we built our design-system
117+
– everything is based off `Box`. This meant that when we used `as` on a styled component it would not be passed down to
118+
the `Box` component. This is because `as` is a prop that styled-components uses to change the underlying element to the
119+
closest `styled` component, but we were using it to change the `tag` prop of `Box`.
120+
121+
We've now introduced the `tag` prop which behaves as we expect.
122+
123+
```diff
124+
import { Link } from 'react-router-dom'
125+
126+
const MyLink = () => {
127+
// the below prop would throw a TS error
128+
- return <BaseLink as={NavLink} to="/home">
129+
// It now, should not.
130+
+ return <BaseLink tag={NavLink} to="/home">
131+
}
132+
```
133+
134+
#### Box, Flex, Grid & Typography are now all react components
96135

97136
These components are no longer styled-components. This means that users can not directly reference them in other styled
98137
components:
@@ -112,6 +151,24 @@ components:
112151
+ `;
113152
```
114153

154+
Because they are now polymoprhic react components that are correctly typed i.e. it will infer the props of the `tag` the
155+
type signature of the component uses generics, `styled-components` has an issue surrounding the poor auto-inference of
156+
this. We therefore recommend you type your `styled` functions where you use them:
157+
158+
```tsx
159+
import { Box, BoxComponent } from '@strapi/design-system';
160+
161+
const MyBox = styled<BoxComponent<'article'>>(Box)`
162+
background-color: red;
163+
`;
164+
165+
const MyComponent = () => {
166+
return <MyBox tag="article">Hello</MyBox>;
167+
};
168+
```
169+
170+
If you don't do this, the props for `MyBox` are all inferred as `Record<string, any>`, which is incorrect.
171+
115172
---
116173

117174
## Icons

packages/strapi-design-system/src/ThemeProvider/ThemeProvider.tsx

Lines changed: 51 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -3,141 +3,76 @@
33
*/
44
import * as React from 'react';
55

6-
import { ThemeProvider as StyledThemeProvider, createGlobalStyle, DefaultTheme } from 'styled-components';
6+
import { ThemeProvider as StyledThemeProvider, createGlobalStyle, DefaultTheme, css } from 'styled-components';
77

88
import { LiveRegions } from '../LiveRegions/LiveRegions';
99
import { lightTheme } from '../themes';
1010

1111
const GlobalStyle = createGlobalStyle`
12-
/* http://meyerweb.com/eric/tools/css/reset/
13-
v2.0 | 20110126
14-
License: none (public domain)
15-
*/
16-
html,
17-
body,
18-
div,
19-
span,
20-
applet,
21-
object,
22-
iframe,
12+
${css`
13+
*,
14+
*::before,
15+
*::after {
16+
box-sizing: border-box;
17+
}
18+
19+
* {
20+
margin: 0;
21+
}
22+
23+
html {
24+
/* Sets 1rem === 10px */
25+
font-size: 62.5%;
26+
}
27+
28+
body {
29+
height: 100%;
30+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans',
31+
'Helvetica Neue', sans-serif;
32+
line-height: 1.5;
33+
-webkit-font-smoothing: antialiased;
34+
}
35+
36+
img,
37+
picture,
38+
video,
39+
canvas,
40+
svg {
41+
display: block;
42+
max-width: 100%;
43+
}
44+
45+
input,
46+
button,
47+
textarea,
48+
select {
49+
font: inherit;
50+
}
51+
52+
p,
2353
h1,
2454
h2,
2555
h3,
2656
h4,
2757
h5,
28-
h6,
29-
p,
30-
blockquote,
31-
pre,
32-
a,
33-
abbr,
34-
acronym,
35-
address,
36-
big,
37-
cite,
38-
code,
39-
del,
40-
dfn,
41-
em,
42-
img,
43-
ins,
44-
kbd,
45-
q,
46-
s,
47-
samp,
48-
small,
49-
strike,
50-
strong,
51-
sub,
52-
sup,
53-
tt,
54-
var,
55-
b,
56-
u,
57-
i,
58-
center,
59-
dl,
60-
dt,
61-
dd,
62-
ol,
63-
ul,
64-
li,
65-
fieldset,
66-
form,
67-
label,
68-
legend,
69-
table,
70-
caption,
71-
tbody,
72-
tfoot,
73-
thead,
74-
tr,
75-
th,
76-
td,
77-
article,
78-
aside,
79-
canvas,
80-
details,
81-
embed,
82-
figure,
83-
figcaption,
84-
footer,
85-
header,
86-
hgroup,
87-
menu,
88-
nav,
89-
output,
90-
ruby,
91-
section,
92-
summary,
93-
time,
94-
mark,
95-
audio,
96-
video {
97-
margin: 0;
98-
padding: 0;
99-
border: 0;
100-
vertical-align: baseline;
58+
h6 {
59+
overflow-wrap: break-word;
60+
font: unset;
10161
}
102-
/* HTML5 display-role reset for older browsers */
103-
article,
104-
aside,
105-
details,
106-
figcaption,
107-
figure,
108-
footer,
109-
header,
110-
hgroup,
111-
menu,
112-
nav,
113-
section {
114-
display: block;
62+
63+
#root {
64+
isolation: isolate;
11565
}
66+
11667
ol,
11768
ul {
11869
list-style: none;
11970
}
120-
blockquote,
121-
q {
122-
quotes: none;
123-
}
124-
blockquote:before,
125-
blockquote:after,
126-
q:before,
127-
q:after {
128-
content: "";
129-
content: none;
130-
}
71+
13172
table {
13273
border-collapse: collapse;
13374
border-spacing: 0;
13475
}
135-
/* My styles */
136-
*,
137-
*:before,
138-
*:after {
139-
box-sizing: border-box;
140-
}
14176
14277
*:focus-visible {
14378
outline: 2px solid ${({ theme }) => theme.colors.primary600};
@@ -149,35 +84,11 @@ const GlobalStyle = createGlobalStyle`
14984
outline: none;
15085
}
15186
152-
html {
153-
/* Sets 1rem === 10px */
154-
font-size: 62.5%;
155-
}
156-
157-
body,html{
158-
height: 100%;
159-
line-height: 1;
160-
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
161-
}
162-
163-
button {
164-
margin: 0;
165-
padding: 0;
166-
border: none;
167-
background: none;
168-
color: inherit;
169-
cursor: pointer;
170-
font: inherit;
171-
}
172-
173-
textarea,input {
174-
font: inherit;
175-
}
176-
17787
.lock-body-scroll {
17888
height: 100vh;
17989
overflow-y: hidden;
18090
}
91+
`}
18192
`;
18293

18394
export interface ThemeProviderProps {

0 commit comments

Comments
 (0)