|
| 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 | +``` |
0 commit comments