Skip to content
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 14 commits into from
Dec 5, 2022
Merged
5 changes: 5 additions & 0 deletions docs/src/data/links.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,11 @@ export const theming: ComponentNavItem[] = [
platforms: ['react', 'vue', 'angular'],
tertiary: true,
},
{
href: '/theming/theme-provider',
label: 'ThemeProvider',
platforms: ['react'],
},
{
href: '/theming/dark-mode',
label: 'Dark mode',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as React from 'react';
import { Card, Text } from '@aws-amplify/ui-react';

export const BasicExample = () => (
// Example using component variations
<Card variation="outlined">
{/* Example using color tokens */}
<Text color="purple.80">Themed purple text</Text>
</Card>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { BasicExample } from './BasicExample';
21 changes: 21 additions & 0 deletions docs/src/pages/[platform]/theming/theme-provider/index.page.mdx
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>
75 changes: 75 additions & 0 deletions docs/src/pages/[platform]/theming/theme-provider/react.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Example, ExampleCode } from '@/components/Example';

import { BasicExample } 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>
);
```

Use the [default theme](./default-theme) to style the components in your app:

<Example>
<BasicExample />

<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`.

- [Theming overview](./theming)
- [Demonstration](./default-theme/colors) of how to use the `theme` prop

### 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you test the link ./dark-mode? It might prevent future errors if we use theming/dark-mode

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I tested the link ./dark-mode


### 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).