-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
docs: replace FAB and Switch screenshots with live interactive examples #5069
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
base: main
Are you sure you want to change the base?
Changes from all commits
150beac
0d28a4a
946bcc9
2d2a8a3
99fc5ff
11022b3
edfcb12
081e3c0
f6b7086
79faefd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import * as React from 'react'; | ||
|
|
||
| import { FAB, type FABSize, type FABVariant } from 'react-native-paper'; | ||
|
|
||
| import InteractiveExample, { ExampleRow, Labelled } from './InteractiveExample'; | ||
|
|
||
| const VARIANTS: FABVariant[] = [ | ||
| 'primary', | ||
| 'secondary', | ||
| 'tertiary', | ||
| 'tonalPrimary', | ||
| 'tonalSecondary', | ||
| 'tonalTertiary', | ||
| ]; | ||
|
|
||
| const SIZES: FABSize[] = ['default', 'medium', 'large']; | ||
|
|
||
| export const FABVariantsExample = () => ( | ||
| <InteractiveExample title="Variants"> | ||
| <ExampleRow> | ||
| {VARIANTS.map((variant) => ( | ||
| <Labelled key={variant} label={variant}> | ||
| <FAB | ||
| icon="pencil" | ||
| variant={variant} | ||
| onPress={() => {}} | ||
| aria-label={`${variant} floating action button`} | ||
| /> | ||
| </Labelled> | ||
| ))} | ||
| </ExampleRow> | ||
| </InteractiveExample> | ||
| ); | ||
|
|
||
| export const FABSizesExample = () => ( | ||
| <InteractiveExample title="Sizes"> | ||
| <ExampleRow> | ||
| {SIZES.map((size) => ( | ||
| <Labelled key={size} label={size}> | ||
| <FAB | ||
| icon="pencil" | ||
| size={size} | ||
| onPress={() => {}} | ||
| aria-label={`${size} floating action button`} | ||
| /> | ||
| </Labelled> | ||
| ))} | ||
| </ExampleRow> | ||
| </InteractiveExample> | ||
| ); | ||
|
|
||
| export const FABExtendedExample = () => { | ||
| const [expanded, setExpanded] = React.useState(true); | ||
|
|
||
| return ( | ||
| <InteractiveExample title="Extended"> | ||
| <FAB.Extended | ||
| icon="plus" | ||
| label="New message" | ||
| expanded={expanded} | ||
| onPress={() => setExpanded((value) => !value)} | ||
| /> | ||
| </InteractiveExample> | ||
| ); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import * as React from 'react'; | ||
| import { StyleSheet, View } from 'react-native'; | ||
|
|
||
| import { DarkTheme, LightTheme, Provider } from 'react-native-paper'; | ||
|
|
||
| import { useColorMode } from './theme-common'; | ||
|
|
||
| /** | ||
| * Row of demo variations, wrapping on narrow viewports. | ||
| */ | ||
| export const ExampleRow = ({ children }: React.PropsWithChildren) => ( | ||
| <View style={styles.row}>{children}</View> | ||
| ); | ||
|
|
||
| /** | ||
| * A single demo variation captioned with the prop value it illustrates. | ||
| */ | ||
| export const Labelled = ({ | ||
| label, | ||
| children, | ||
| }: React.PropsWithChildren<{ label: string }>) => ( | ||
| <View style={styles.item}> | ||
| {children} | ||
| <span className="paper-interactive-example__label">{label}</span> | ||
| </View> | ||
| ); | ||
|
|
||
| type InteractiveExampleProps = React.PropsWithChildren<{ | ||
| /** | ||
| * Short caption rendered above the live preview, describing what the | ||
| * example demonstrates. | ||
| */ | ||
| title?: string; | ||
| }>; | ||
|
|
||
| /** | ||
| * Shared shell for live component demos embedded in the docs. | ||
| * | ||
| * The Paper theme follows the active docs color mode so demos match the | ||
| * surrounding page. | ||
| */ | ||
| const InteractiveExample = ({ title, children }: InteractiveExampleProps) => { | ||
| const isDarkTheme = useColorMode().colorMode === 'dark'; | ||
|
|
||
| return ( | ||
| <Provider theme={isDarkTheme ? DarkTheme : LightTheme}> | ||
|
Collaborator
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. Have you checked these in dark mode? I'm afraid they render with light-theme colors, so the caption under each switch ends up dark on dark and unreadable.
Collaborator
Author
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. |
||
| <figure className="paper-interactive-example"> | ||
| {title ? ( | ||
| <figcaption className="paper-interactive-example__title"> | ||
| {title} | ||
| </figcaption> | ||
| ) : null} | ||
| <View style={styles.content}>{children}</View> | ||
| </figure> | ||
| </Provider> | ||
| ); | ||
| }; | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| content: { | ||
| alignItems: 'flex-start', | ||
| }, | ||
| row: { | ||
| flexDirection: 'row', | ||
| flexWrap: 'wrap', | ||
| alignItems: 'center', | ||
| gap: 24, | ||
| }, | ||
| item: { | ||
| alignItems: 'center', | ||
| gap: 8, | ||
| }, | ||
| }); | ||
|
|
||
| export default InteractiveExample; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import * as React from 'react'; | ||
|
|
||
| import { Switch } from 'react-native-paper'; | ||
|
|
||
| import InteractiveExample, { ExampleRow, Labelled } from './InteractiveExample'; | ||
|
|
||
| export const SwitchStatesExample = () => { | ||
| const [on, setOn] = React.useState(true); | ||
| const [off, setOff] = React.useState(false); | ||
|
|
||
| return ( | ||
| <InteractiveExample title="Enabled"> | ||
| <ExampleRow> | ||
| <Labelled label="selected"> | ||
| <Switch value={on} onValueChange={setOn} aria-label="Selected" /> | ||
| </Labelled> | ||
| <Labelled label="unselected"> | ||
| <Switch value={off} onValueChange={setOff} aria-label="Unselected" /> | ||
| </Labelled> | ||
| </ExampleRow> | ||
| </InteractiveExample> | ||
| ); | ||
| }; | ||
|
|
||
| export const SwitchDisabledExample = () => ( | ||
| <InteractiveExample title="Disabled"> | ||
| <ExampleRow> | ||
| <Labelled label="selected"> | ||
| <Switch value disabled aria-label="Disabled and selected" /> | ||
| </Labelled> | ||
| <Labelled label="unselected"> | ||
| <Switch value={false} disabled aria-label="Disabled and unselected" /> | ||
| </Labelled> | ||
| </ExampleRow> | ||
| </InteractiveExample> | ||
| ); | ||
|
|
||
| export const SwitchIconsExample = () => { | ||
| const [on, setOn] = React.useState(true); | ||
| const [off, setOff] = React.useState(false); | ||
|
|
||
| return ( | ||
| <InteractiveExample title="With icons in the handle"> | ||
| <ExampleRow> | ||
| <Labelled label="selected"> | ||
| <Switch | ||
| value={on} | ||
| onValueChange={setOn} | ||
| checkedIcon="check" | ||
| uncheckedIcon="close" | ||
| aria-label="Selected with icon" | ||
| /> | ||
| </Labelled> | ||
| <Labelled label="unselected"> | ||
| <Switch | ||
| value={off} | ||
| onValueChange={setOff} | ||
| checkedIcon="check" | ||
| uncheckedIcon="close" | ||
| aria-label="Unselected with icon" | ||
| /> | ||
| </Labelled> | ||
| </ExampleRow> | ||
| </InteractiveExample> | ||
| ); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| export type LiveExample = { | ||
| /** Module specifier the examples are imported from. */ | ||
| module: string; | ||
| /** Named exports rendered, in order, under the page summary. */ | ||
| exports: string[]; | ||
| }; | ||
|
|
||
| export const liveExamples: Record<string, LiveExample> = { | ||
| FAB: { | ||
| module: '@docs/components/FABExample.tsx', | ||
| exports: ['FABVariantsExample', 'FABSizesExample'], | ||
| }, | ||
| Extended: { | ||
| module: '@docs/components/FABExample.tsx', | ||
| exports: ['FABExtendedExample'], | ||
| }, | ||
| Switch: { | ||
| module: '@docs/components/SwitchExample.tsx', | ||
| exports: [ | ||
| 'SwitchStatesExample', | ||
| 'SwitchDisabledExample', | ||
| 'SwitchIconsExample', | ||
| ], | ||
| }, | ||
| }; |

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.
Have you checked the extended FAB on the built page?
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.
Yes, checked it on the built output (yarn build + yarn preview), it renders correctly, and the interaction is as expected
