Skip to content

Commit 60d9095

Browse files
dbanksdesigncalebpollmanjordanvn
committed
feat(ui): component theming (#5170)
Co-authored-by: Caleb Pollman <[email protected]> Co-authored-by: Jordan Van Ness <[email protected]>
1 parent 3afb824 commit 60d9095

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

.changeset/rad-cat-shred.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
"@aws-amplify/ui": minor
3+
"@aws-amplify/ui-react": minor
4+
---
5+
6+
feat(ui): experimental component theming
7+
8+
This feature lets you fully style and theme built-in components even if there is no design token available. For example, previously you could not add a box shadow or gradient background to the built-in Button component unless you wrote plain CSS. Now you can style every CSS property for all the built-in components with type-safety!
9+
10+
This also lets you define your own components and style them in the same type-safe way with zero runtime computation.
11+
12+
### defineComponentTheme()
13+
14+
```ts
15+
import { defineComponentTheme } from '@aws-amplify/ui-react/server';
16+
17+
export const buttonTheme = defineComponentTheme({
18+
// because 'button' is a built-in component, we get type-safety and hints
19+
// based on the theme shape of our button
20+
name: 'button',
21+
theme: (tokens) => {
22+
return {
23+
textAlign: 'center',
24+
padding: tokens.space.xl,
25+
_modifiers: {
26+
primary: {
27+
backgroundColor: tokens.colors.primary[20],
28+
},
29+
},
30+
};
31+
},
32+
});
33+
```
34+
35+
36+
### createTheme()
37+
38+
The theme object passed to `createTheme` now has an optional `components` array which is an array of component themes.
39+
40+
```ts
41+
export const theme = createTheme({
42+
name: 'my-theme',
43+
components: [
44+
buttonTheme,
45+
customComponentTheme,
46+
]
47+
})
48+
```
49+
50+
### React Server Component support for theming
51+
52+
You no longer need to use the `<ThemeProvider>` and rely on React context to theme Amplify UI (you still can though!). There is a new import path for RSC-compliant code: '@aws-amplify/ui-react/server' which you can use to import `createTheme` and `defineComponentTheme` as well as a new React Server Component: `<ThemeStyle />` which will inject the styles of your theme into the page.
53+
54+
55+
```tsx
56+
import { ThemeStyle, createTheme } from '@aws-amplify/ui-react/server';
57+
58+
const theme = createTheme({
59+
//...
60+
})
61+
62+
export default function RootLayout({
63+
children,
64+
}: {
65+
children: React.ReactNode;
66+
}) {
67+
return (
68+
<div {...theme.containerProps({ colorMode: 'system' })}>
69+
{children}
70+
<ThemeStyle theme={theme} />
71+
</div>
72+
)
73+
}
74+
```

packages/react/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
"import": "./dist/esm/server.mjs",
2020
"require": "./dist/server.js"
2121
},
22+
"./server": {
23+
"types": "./dist/types/server.d.ts",
24+
"import": "./dist/esm/server.mjs",
25+
"require": "./dist/server.js"
26+
},
2227
"./styles.css": "./dist/styles.css",
2328
"./styles.layer.css": "./dist/styles.layer.css",
2429
"./styles/*": "./dist/styles/*",

0 commit comments

Comments
 (0)