-
Notifications
You must be signed in to change notification settings - Fork 319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(docs): ThemeProvider API documentation #2711
Merged
Merged
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0e11b1e
add new docs page for ThemeProvider API
38b69f1
rename ThemeProvider API to ThemeProvider
9ba9f8b
document nonce prop for ThemeProvider
be88923
include basic ThemeProvider example
a99909c
Merge branch 'main' into docs-themeprovider
joebuono ea5caa3
Merge branch 'main' into docs-themeprovider
joebuono 3cb8c8a
Merge branch 'main' into docs-themeprovider
60b61dc
Merge branch 'main' into docs-themeprovider
joebuono 7e5ef0d
Merge branch 'docs-themeprovider' of github.com:aws-amplify/amplify-u…
a9755bd
update theming example
1fde086
update custom theme example
9635158
Update docs/src/pages/[platform]/theming/theme-provider/react.mdx
joebuono 61746b4
remove useTheme hook from custom theme example
00afb23
Merge branch 'main' into docs-themeprovider
joebuono File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
docs/src/pages/[platform]/theming/theme-provider/examples/BasicExample.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Button, useTheme } from '@aws-amplify/ui-react'; | ||
|
||
export const BasicExample = () => { | ||
const { tokens } = useTheme(); | ||
|
||
return ( | ||
<Button border={`2px solid ${tokens.colors.blue[80]}`} color="purple.80"> | ||
Themed Button | ||
</Button> | ||
); | ||
}; |
38 changes: 38 additions & 0 deletions
38
docs/src/pages/[platform]/theming/theme-provider/examples/CustomThemeExample.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { | ||
Card, | ||
Heading, | ||
Text, | ||
ThemeProvider, | ||
useTheme, | ||
} from '@aws-amplify/ui-react'; | ||
|
||
export const CustomThemeExample = () => { | ||
const { tokens } = useTheme(); | ||
joebuono marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const theme = { | ||
name: 'custom-theme', | ||
tokens: { | ||
components: { | ||
card: { | ||
backgroundColor: tokens.colors.background.secondary, | ||
outlined: { | ||
borderColor: tokens.colors.black, | ||
}, | ||
}, | ||
heading: { | ||
color: tokens.colors.brand.secondary[80], | ||
}, | ||
text: { | ||
color: tokens.colors.neutral[80], | ||
}, | ||
}, | ||
}, | ||
}; | ||
return ( | ||
<ThemeProvider theme={theme}> | ||
<Card variation="outlined"> | ||
<Heading level={6}>Heading text</Heading> | ||
<Text>Some sample text for this card.</Text> | ||
</Card> | ||
</ThemeProvider> | ||
); | ||
}; |
2 changes: 2 additions & 0 deletions
2
docs/src/pages/[platform]/theming/theme-provider/examples/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { BasicExample } from './BasicExample'; | ||
export { CustomThemeExample } from './CustomThemeExample'; |
21 changes: 21 additions & 0 deletions
21
docs/src/pages/[platform]/theming/theme-provider/index.page.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
title: ThemeProvider | ||
description: The ThemeProvider allows you to apply a Theme to your application. | ||
supportedFrameworks: react | ||
--- | ||
|
||
import { Fragment } from '@/components/Fragment'; | ||
import { FRAMEWORKS } from '@/data/frameworks'; | ||
import { getCustomStaticPath } from "@/utils/getCustomStaticPath"; | ||
|
||
export async function getStaticPaths() { | ||
return getCustomStaticPath(frontmatter.supportedFrameworks); | ||
} | ||
|
||
{/* `getStaticProps` is required to prevent "Error: getStaticPaths was added without a getStaticProps. Without getStaticProps, getStaticPaths does nothing" */} | ||
|
||
export async function getStaticProps() { | ||
return { props: {} } | ||
} | ||
|
||
<Fragment>{({ platform }) => import(`./${platform}.mdx`)}</Fragment> |
89 changes: 89 additions & 0 deletions
89
docs/src/pages/[platform]/theming/theme-provider/react.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { Flex } from '@aws-amplify/ui-react'; | ||
import { Example, ExampleCode } from '@/components/Example'; | ||
|
||
import { BasicExample, CustomThemeExample } from './examples'; | ||
|
||
## Usage | ||
|
||
Import the `ThemeProvider` and wrap your application with it: | ||
|
||
```jsx | ||
import { ThemeProvider } from '@aws-amplify/ui-react'; | ||
|
||
export const App = () => ( | ||
<ThemeProvider> | ||
<YourApplication /> | ||
</ThemeProvider> | ||
); | ||
``` | ||
|
||
After wrapping your application in the `ThemeProvider`, you have access to theme values in your components. To use the [default theme](./default-theme) to style the components in your app, you can either: | ||
1. Get the theme `tokens` through the `useTheme` hook (e.g., `tokens.colors.blue[80]`) | ||
2. Reference the theme `tokens` directly in style props (e.g., `"purple.80"`) | ||
joebuono marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
<Example> | ||
<Flex> | ||
<BasicExample /> | ||
</Flex> | ||
|
||
<ExampleCode> | ||
```tsx file=./examples/BasicExample.tsx | ||
``` | ||
|
||
</ExampleCode> | ||
</Example> | ||
|
||
### theme | ||
|
||
To create and use your own custom theme, you may pass a [theme object](https://ui.docs.amplify.aws/react/theming#theme-object) to the `theme` prop on the `ThemeProvider`. | ||
|
||
joebuono marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- [Theming overview](./theming) | ||
|
||
<Example> | ||
<CustomThemeExample /> | ||
|
||
<ExampleCode> | ||
```tsx file=./examples/CustomThemeExample.tsx | ||
``` | ||
|
||
</ExampleCode> | ||
</Example> | ||
|
||
### colorMode | ||
|
||
The `ThemeProvider` accepts a `colorMode` prop which can be `light`, `dark`, or `system`. | ||
|
||
See the [Dark mode documentation](./dark-mode) for a detailed explanation of how to use the `colorMode` prop. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you test the link There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I tested the link |
||
|
||
### nonce | ||
|
||
When you have a `Content-Security-Policy` ([CSP](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP)) header defined, the browser will automatically block inline styles. | ||
|
||
To safely allow inline styles when using strict CSP rules, you may pass a [nonce](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce) to the `nonce` prop on the `ThemeProvider`. This will add a nonce to the `<style>` tag rendered by the `ThemeProvider`. For example: | ||
|
||
**CSP rules** | ||
``` | ||
style-src css-cdn.example.com 'nonce-rAnd0m'; | ||
``` | ||
|
||
**ThemeProvider** | ||
```jsx | ||
<ThemeProvider nonce="rAnd0m"> | ||
{/* your app */} | ||
</ThemeProvider> | ||
``` | ||
|
||
**HTML output** | ||
```html | ||
<style nonce="rAnd0m"> | ||
:root, [data-amplify-theme] { | ||
--amplify-colors-white: hsl(0, 0%, 100%); | ||
/* etc */ | ||
} | ||
/* | ||
* Any of your custom theme styles | ||
*/ | ||
</style> | ||
``` | ||
|
||
For more information, see the following documention on [allowing inline styles using a nonce](https://content-security-policy.com/examples/allow-inline-style). |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here, in the same line, why have
blue[80]
butpurple.80
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the style props (e.g.,
purple.80
), it won't work if you use bracket syntax. And for the theme tokens, it won't work if you use dot syntax.