@coinbase/cds-mcp-server 8.17.3 → 8.17.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/mcp-docs/mobile/components/Accordion.txt +11 -12
- package/mcp-docs/mobile/components/AccordionItem.txt +5 -6
- package/mcp-docs/mobile/components/Alert.txt +21 -22
- package/mcp-docs/mobile/components/AreaChart.txt +118 -119
- package/mcp-docs/mobile/components/Avatar.txt +82 -83
- package/mcp-docs/mobile/components/AvatarButton.txt +103 -104
- package/mcp-docs/mobile/components/Banner.txt +100 -101
- package/mcp-docs/mobile/components/BarChart.txt +120 -121
- package/mcp-docs/mobile/components/Box.txt +65 -66
- package/mcp-docs/mobile/components/BrowserBar.txt +38 -39
- package/mcp-docs/mobile/components/Button.txt +78 -79
- package/mcp-docs/mobile/components/ButtonGroup.txt +10 -11
- package/mcp-docs/mobile/components/Carousel.txt +103 -104
- package/mcp-docs/mobile/components/CartesianChart.txt +107 -108
- package/mcp-docs/mobile/components/CellMedia.txt +15 -16
- package/mcp-docs/mobile/components/Checkbox.txt +35 -36
- package/mcp-docs/mobile/components/CheckboxCell.txt +73 -74
- package/mcp-docs/mobile/components/CheckboxGroup.txt +24 -25
- package/mcp-docs/mobile/components/Chip.txt +67 -68
- package/mcp-docs/mobile/components/Coachmark.txt +42 -43
- package/mcp-docs/mobile/components/Collapsible.txt +15 -16
- package/mcp-docs/mobile/components/ContainedAssetCard.txt +16 -17
- package/mcp-docs/mobile/components/ContentCard.txt +99 -100
- package/mcp-docs/mobile/components/ContentCardBody.txt +23 -24
- package/mcp-docs/mobile/components/ContentCardFooter.txt +19 -20
- package/mcp-docs/mobile/components/ContentCardHeader.txt +33 -34
- package/mcp-docs/mobile/components/ContentCell.txt +102 -103
- package/mcp-docs/mobile/components/ControlGroup.txt +106 -107
- package/mcp-docs/mobile/components/DatePicker.txt +121 -122
- package/mcp-docs/mobile/components/Divider.txt +31 -32
- package/mcp-docs/mobile/components/DotCount.txt +15 -16
- package/mcp-docs/mobile/components/DotStatusColor.txt +12 -13
- package/mcp-docs/mobile/components/DotSymbol.txt +21 -22
- package/mcp-docs/mobile/components/Fallback.txt +49 -50
- package/mcp-docs/mobile/components/FloatingAssetCard.txt +13 -14
- package/mcp-docs/mobile/components/HStack.txt +99 -100
- package/mcp-docs/mobile/components/HeroSquare.txt +11 -12
- package/mcp-docs/mobile/components/Icon.txt +16 -17
- package/mcp-docs/mobile/components/IconButton.txt +104 -105
- package/mcp-docs/mobile/components/InputChip.txt +62 -63
- package/mcp-docs/mobile/components/Interactable.txt +72 -73
- package/mcp-docs/mobile/components/LineChart.txt +117 -118
- package/mcp-docs/mobile/components/Link.txt +113 -114
- package/mcp-docs/mobile/components/ListCell.txt +149 -128
- package/mcp-docs/mobile/components/LogoMark.txt +8 -9
- package/mcp-docs/mobile/components/LogoWordMark.txt +7 -8
- package/mcp-docs/mobile/components/Lottie.txt +39 -40
- package/mcp-docs/mobile/components/LottieStatusAnimation.txt +11 -12
- package/mcp-docs/mobile/components/Modal.txt +36 -37
- package/mcp-docs/mobile/components/ModalBody.txt +5 -6
- package/mcp-docs/mobile/components/ModalFooter.txt +5 -6
- package/mcp-docs/mobile/components/ModalHeader.txt +5 -6
- package/mcp-docs/mobile/components/MultiContentModule.txt +107 -108
- package/mcp-docs/mobile/components/NavigationTitle.txt +13 -14
- package/mcp-docs/mobile/components/NavigationTitleSelect.txt +20 -21
- package/mcp-docs/mobile/components/NudgeCard.txt +20 -21
- package/mcp-docs/mobile/components/Numpad.txt +108 -109
- package/mcp-docs/mobile/components/Overlay.txt +45 -46
- package/mcp-docs/mobile/components/PageFooter.txt +57 -58
- package/mcp-docs/mobile/components/PageHeader.txt +79 -80
- package/mcp-docs/mobile/components/PeriodSelector.txt +98 -99
- package/mcp-docs/mobile/components/Pictogram.txt +11 -12
- package/mcp-docs/mobile/components/Point.txt +21 -22
- package/mcp-docs/mobile/components/PortalProvider.txt +7 -8
- package/mcp-docs/mobile/components/Pressable.txt +67 -68
- package/mcp-docs/mobile/components/ProgressBar.txt +18 -19
- package/mcp-docs/mobile/components/ProgressBarWithFixedLabels.txt +13 -14
- package/mcp-docs/mobile/components/ProgressBarWithFloatLabel.txt +13 -14
- package/mcp-docs/mobile/components/ProgressCircle.txt +22 -23
- package/mcp-docs/mobile/components/Radio.txt +35 -36
- package/mcp-docs/mobile/components/RadioCell.txt +73 -74
- package/mcp-docs/mobile/components/RadioGroup.txt +35 -36
- package/mcp-docs/mobile/components/ReferenceLine.txt +15 -16
- package/mcp-docs/mobile/components/RemoteImage.txt +47 -48
- package/mcp-docs/mobile/components/RemoteImageGroup.txt +11 -12
- package/mcp-docs/mobile/components/RollingNumber.txt +130 -131
- package/mcp-docs/mobile/components/Scrubber.txt +19 -20
- package/mcp-docs/mobile/components/SearchInput.txt +77 -78
- package/mcp-docs/mobile/components/SectionHeader.txt +16 -17
- package/mcp-docs/mobile/components/SegmentedTabs.txt +105 -106
- package/mcp-docs/mobile/components/Select.txt +35 -36
- package/mcp-docs/mobile/components/SelectChip.txt +143 -144
- package/mcp-docs/mobile/components/SelectOption.txt +26 -27
- package/mcp-docs/mobile/components/SlideButton.txt +128 -129
- package/mcp-docs/mobile/components/Spacer.txt +27 -28
- package/mcp-docs/mobile/components/Sparkline.txt +18 -19
- package/mcp-docs/mobile/components/SparklineGradient.txt +18 -19
- package/mcp-docs/mobile/components/SparklineInteractive.txt +37 -38
- package/mcp-docs/mobile/components/SparklineInteractiveHeader.txt +12 -13
- package/mcp-docs/mobile/components/Spinner.txt +15 -16
- package/mcp-docs/mobile/components/SpotIcon.txt +11 -12
- package/mcp-docs/mobile/components/SpotRectangle.txt +11 -12
- package/mcp-docs/mobile/components/SpotSquare.txt +11 -12
- package/mcp-docs/mobile/components/Stepper.txt +113 -114
- package/mcp-docs/mobile/components/SubBrandLogoMark.txt +8 -9
- package/mcp-docs/mobile/components/SubBrandLogoWordMark.txt +8 -9
- package/mcp-docs/mobile/components/Switch.txt +35 -36
- package/mcp-docs/mobile/components/TabIndicator.txt +12 -13
- package/mcp-docs/mobile/components/TabLabel.txt +28 -29
- package/mcp-docs/mobile/components/TabNavigation.txt +41 -42
- package/mcp-docs/mobile/components/TabbedChips.txt +38 -39
- package/mcp-docs/mobile/components/Tabs.txt +76 -77
- package/mcp-docs/mobile/components/Tag.txt +102 -103
- package/mcp-docs/mobile/components/Text.txt +91 -92
- package/mcp-docs/mobile/components/TextInput.txt +107 -108
- package/mcp-docs/mobile/components/ThemeProvider.txt +8 -9
- package/mcp-docs/mobile/components/Toast.txt +82 -83
- package/mcp-docs/mobile/components/Tooltip.txt +20 -21
- package/mcp-docs/mobile/components/TopNavBar.txt +55 -56
- package/mcp-docs/mobile/components/Tour.txt +18 -19
- package/mcp-docs/mobile/components/Tray.txt +43 -44
- package/mcp-docs/mobile/components/UpsellCard.txt +16 -17
- package/mcp-docs/mobile/components/VStack.txt +99 -100
- package/mcp-docs/mobile/components/XAxis.txt +25 -26
- package/mcp-docs/mobile/components/YAxis.txt +26 -27
- package/mcp-docs/mobile/getting-started/{mcp-server.txt → ai-overview.txt} +51 -37
- package/mcp-docs/mobile/getting-started/installation.txt +57 -0
- package/mcp-docs/mobile/getting-started/introduction.txt +3 -0
- package/mcp-docs/mobile/getting-started/playground.txt +3 -0
- package/mcp-docs/mobile/getting-started/styling.txt +84 -0
- package/mcp-docs/mobile/getting-started/theming.txt +286 -0
- package/mcp-docs/mobile/hooks/useDimensions.txt +27 -2
- package/mcp-docs/mobile/hooks/useEventHandler.txt +120 -0
- package/mcp-docs/mobile/hooks/useMergeRefs.txt +116 -0
- package/mcp-docs/mobile/hooks/useOverlayContentContext.txt +67 -2
- package/mcp-docs/mobile/hooks/usePreviousValue.txt +74 -0
- package/mcp-docs/mobile/hooks/useRefMap.txt +178 -0
- package/mcp-docs/mobile/hooks/useTheme.txt +213 -2
- package/mcp-docs/mobile/routes.txt +8 -1
- package/mcp-docs/web/components/Accordion.txt +12 -13
- package/mcp-docs/web/components/AccordionItem.txt +5 -6
- package/mcp-docs/web/components/Alert.txt +24 -25
- package/mcp-docs/web/components/AreaChart.txt +137 -138
- package/mcp-docs/web/components/Avatar.txt +91 -92
- package/mcp-docs/web/components/AvatarButton.txt +113 -114
- package/mcp-docs/web/components/Banner.txt +101 -102
- package/mcp-docs/web/components/BarChart.txt +367 -368
- package/mcp-docs/web/components/Box.txt +65 -66
- package/mcp-docs/web/components/Button.txt +81 -82
- package/mcp-docs/web/components/ButtonGroup.txt +10 -11
- package/mcp-docs/web/components/Calendar.txt +56 -57
- package/mcp-docs/web/components/Carousel.txt +116 -117
- package/mcp-docs/web/components/CartesianChart.txt +357 -358
- package/mcp-docs/web/components/CellMedia.txt +14 -15
- package/mcp-docs/web/components/Checkbox.txt +22 -23
- package/mcp-docs/web/components/CheckboxCell.txt +70 -71
- package/mcp-docs/web/components/CheckboxGroup.txt +15 -16
- package/mcp-docs/web/components/Chip.txt +67 -68
- package/mcp-docs/web/components/Coachmark.txt +69 -70
- package/mcp-docs/web/components/Collapsible.txt +15 -16
- package/mcp-docs/web/components/ContainedAssetCard.txt +108 -109
- package/mcp-docs/web/components/ContentCard.txt +101 -102
- package/mcp-docs/web/components/ContentCardBody.txt +23 -24
- package/mcp-docs/web/components/ContentCardFooter.txt +19 -20
- package/mcp-docs/web/components/ContentCardHeader.txt +33 -34
- package/mcp-docs/web/components/ContentCell.txt +85 -86
- package/mcp-docs/web/components/ControlGroup.txt +108 -109
- package/mcp-docs/web/components/DatePicker.txt +59 -60
- package/mcp-docs/web/components/Divider.txt +31 -32
- package/mcp-docs/web/components/DotCount.txt +17 -18
- package/mcp-docs/web/components/DotStatusColor.txt +12 -13
- package/mcp-docs/web/components/DotSymbol.txt +24 -25
- package/mcp-docs/web/components/Dropdown.txt +31 -32
- package/mcp-docs/web/components/Fallback.txt +49 -50
- package/mcp-docs/web/components/FloatingAssetCard.txt +108 -109
- package/mcp-docs/web/components/FullscreenAlert.txt +20 -21
- package/mcp-docs/web/components/FullscreenModal.txt +28 -29
- package/mcp-docs/web/components/FullscreenModalLayout.txt +19 -20
- package/mcp-docs/web/components/Grid.txt +106 -107
- package/mcp-docs/web/components/GridColumn.txt +96 -97
- package/mcp-docs/web/components/HStack.txt +101 -102
- package/mcp-docs/web/components/HeroSquare.txt +12 -13
- package/mcp-docs/web/components/Icon.txt +27 -28
- package/mcp-docs/web/components/IconButton.txt +117 -118
- package/mcp-docs/web/components/InputChip.txt +62 -63
- package/mcp-docs/web/components/Interactable.txt +75 -76
- package/mcp-docs/web/components/LineChart.txt +366 -367
- package/mcp-docs/web/components/Link.txt +114 -115
- package/mcp-docs/web/components/ListCell.txt +157 -136
- package/mcp-docs/web/components/LogoMark.txt +8 -9
- package/mcp-docs/web/components/LogoWordMark.txt +7 -8
- package/mcp-docs/web/components/Lottie.txt +44 -45
- package/mcp-docs/web/components/LottieStatusAnimation.txt +11 -12
- package/mcp-docs/web/components/MediaQueryProvider.txt +7 -8
- package/mcp-docs/web/components/Modal.txt +69 -66
- package/mcp-docs/web/components/ModalBody.txt +5 -6
- package/mcp-docs/web/components/ModalFooter.txt +5 -6
- package/mcp-docs/web/components/ModalHeader.txt +5 -6
- package/mcp-docs/web/components/MultiContentModule.txt +109 -110
- package/mcp-docs/web/components/NavigationBar.txt +18 -19
- package/mcp-docs/web/components/NavigationTitle.txt +7 -8
- package/mcp-docs/web/components/NavigationTitleSelect.txt +9 -10
- package/mcp-docs/web/components/NudgeCard.txt +61 -62
- package/mcp-docs/web/components/Overlay.txt +57 -58
- package/mcp-docs/web/components/PageFooter.txt +77 -78
- package/mcp-docs/web/components/PageHeader.txt +107 -108
- package/mcp-docs/web/components/Pagination.txt +115 -116
- package/mcp-docs/web/components/PeriodSelector.txt +158 -159
- package/mcp-docs/web/components/Pictogram.txt +13 -14
- package/mcp-docs/web/components/Point.txt +169 -170
- package/mcp-docs/web/components/PortalProvider.txt +6 -7
- package/mcp-docs/web/components/Pressable.txt +73 -74
- package/mcp-docs/web/components/ProgressBar.txt +20 -21
- package/mcp-docs/web/components/ProgressBarWithFixedLabels.txt +15 -16
- package/mcp-docs/web/components/ProgressBarWithFloatLabel.txt +15 -16
- package/mcp-docs/web/components/ProgressCircle.txt +24 -25
- package/mcp-docs/web/components/Radio.txt +22 -23
- package/mcp-docs/web/components/RadioCell.txt +83 -84
- package/mcp-docs/web/components/RadioGroup.txt +22 -23
- package/mcp-docs/web/components/ReferenceLine.txt +22 -23
- package/mcp-docs/web/components/RemoteImage.txt +49 -50
- package/mcp-docs/web/components/RemoteImageGroup.txt +11 -12
- package/mcp-docs/web/components/RollingNumber.txt +131 -132
- package/mcp-docs/web/components/Scrubber.txt +22 -23
- package/mcp-docs/web/components/SearchInput.txt +29 -30
- package/mcp-docs/web/components/SectionHeader.txt +98 -99
- package/mcp-docs/web/components/SegmentedTabs.txt +109 -110
- package/mcp-docs/web/components/Select.txt +22 -23
- package/mcp-docs/web/components/SelectChip.txt +134 -135
- package/mcp-docs/web/components/SelectOption.txt +31 -32
- package/mcp-docs/web/components/Sidebar.txt +109 -110
- package/mcp-docs/web/components/SidebarItem.txt +6 -7
- package/mcp-docs/web/components/SidebarMoreMenu.txt +5 -6
- package/mcp-docs/web/components/Spacer.txt +57 -58
- package/mcp-docs/web/components/Sparkline.txt +18 -19
- package/mcp-docs/web/components/SparklineGradient.txt +18 -19
- package/mcp-docs/web/components/SparklineInteractive.txt +34 -35
- package/mcp-docs/web/components/SparklineInteractiveHeader.txt +12 -13
- package/mcp-docs/web/components/Spinner.txt +15 -16
- package/mcp-docs/web/components/SpotIcon.txt +12 -13
- package/mcp-docs/web/components/SpotRectangle.txt +12 -13
- package/mcp-docs/web/components/SpotSquare.txt +12 -13
- package/mcp-docs/web/components/Stepper.txt +119 -120
- package/mcp-docs/web/components/SubBrandLogoMark.txt +8 -9
- package/mcp-docs/web/components/SubBrandLogoWordMark.txt +8 -9
- package/mcp-docs/web/components/Switch.txt +23 -24
- package/mcp-docs/web/components/TabIndicator.txt +12 -13
- package/mcp-docs/web/components/TabLabel.txt +28 -29
- package/mcp-docs/web/components/TabNavigation.txt +41 -42
- package/mcp-docs/web/components/TabbedChips.txt +38 -39
- package/mcp-docs/web/components/Table.txt +17 -18
- package/mcp-docs/web/components/TableBody.txt +9 -10
- package/mcp-docs/web/components/TableCaption.txt +13 -14
- package/mcp-docs/web/components/TableCell.txt +21 -22
- package/mcp-docs/web/components/TableCellFallback.txt +16 -17
- package/mcp-docs/web/components/TableFooter.txt +9 -10
- package/mcp-docs/web/components/TableHeader.txt +10 -11
- package/mcp-docs/web/components/TableRow.txt +13 -14
- package/mcp-docs/web/components/Tabs.txt +94 -95
- package/mcp-docs/web/components/Tag.txt +106 -107
- package/mcp-docs/web/components/Text.txt +110 -111
- package/mcp-docs/web/components/TextInput.txt +32 -33
- package/mcp-docs/web/components/ThemeProvider.txt +27 -28
- package/mcp-docs/web/components/TileButton.txt +37 -38
- package/mcp-docs/web/components/Toast.txt +82 -83
- package/mcp-docs/web/components/Tooltip.txt +22 -23
- package/mcp-docs/web/components/Tour.txt +21 -22
- package/mcp-docs/web/components/Tray.txt +23 -24
- package/mcp-docs/web/components/UpsellCard.txt +16 -17
- package/mcp-docs/web/components/VStack.txt +101 -102
- package/mcp-docs/web/components/XAxis.txt +25 -26
- package/mcp-docs/web/components/YAxis.txt +26 -27
- package/mcp-docs/web/getting-started/{mcp-server.txt → ai-overview.txt} +51 -37
- package/mcp-docs/web/getting-started/installation.txt +103 -0
- package/mcp-docs/web/getting-started/introduction.txt +3 -0
- package/mcp-docs/web/getting-started/playground.txt +3 -0
- package/mcp-docs/web/getting-started/styling.txt +161 -0
- package/mcp-docs/web/getting-started/templates.txt +121 -0
- package/mcp-docs/web/getting-started/theming.txt +426 -0
- package/mcp-docs/web/hooks/useBreakpoints.txt +30 -2
- package/mcp-docs/web/hooks/useDimensions.txt +61 -2
- package/mcp-docs/web/hooks/useEventHandler.txt +120 -0
- package/mcp-docs/web/hooks/useHasMounted.txt +22 -2
- package/mcp-docs/web/hooks/useIsoEffect.txt +18 -2
- package/mcp-docs/web/hooks/useMediaQuery.txt +22 -2
- package/mcp-docs/web/hooks/useMergeRefs.txt +116 -0
- package/mcp-docs/web/hooks/useOverlayContentContext.txt +64 -2
- package/mcp-docs/web/hooks/usePreviousValue.txt +74 -0
- package/mcp-docs/web/hooks/useRefMap.txt +178 -0
- package/mcp-docs/web/hooks/useScrollBlocker.txt +21 -2
- package/mcp-docs/web/hooks/useTheme.txt +261 -2
- package/mcp-docs/web/routes.txt +9 -1
- package/package.json +1 -1
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
# theming
|
|
2
|
+
|
|
3
|
+
The theme contains design tokens like colors, fonts, spacing, and more. The ThemeProvider provides access to these values via CSS Variables for web, and React Context for both web and React Native.
|
|
4
|
+
|
|
5
|
+
import { JSONCodeBlock } from '@site/src/components/page/JSONCodeBlock';
|
|
6
|
+
import { defaultTheme } from '@coinbase/cds-web/themes/defaultTheme';
|
|
7
|
+
import { createThemeCssVars } from '@coinbase/cds-web/core/createThemeCssVars';
|
|
8
|
+
### ThemeProvider component
|
|
9
|
+
|
|
10
|
+
The ThemeProvider provides the theme context to all child components, and automatically generates CSS Variables for dynamic theming.
|
|
11
|
+
|
|
12
|
+
You must pass the `theme` prop to configure the theme, and the `activeColorScheme` prop to set light or dark mode.
|
|
13
|
+
|
|
14
|
+
[See the ThemeProvider docs here →](/components/other/ThemeProvider)
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
import { ThemeProvider } from '@coinbase/cds-web/system/ThemeProvider';
|
|
18
|
+
import { defaultTheme } from '@coinbase/cds-web/themes/defaultTheme';
|
|
19
|
+
|
|
20
|
+
const App = () => (
|
|
21
|
+
<ThemeProvider theme={defaultTheme} activeColorScheme="light">
|
|
22
|
+
{/* Your app components */}
|
|
23
|
+
</ThemeProvider>
|
|
24
|
+
);
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
:::tip
|
|
28
|
+
Changing the `activeColorScheme` automatically updates the values returned from the `useTheme` hook and from CSS Variables.
|
|
29
|
+
:::
|
|
30
|
+
|
|
31
|
+
#### `useTheme` hook
|
|
32
|
+
|
|
33
|
+
The `useTheme` hook provides access to the current `theme` and `activeColorScheme`.
|
|
34
|
+
|
|
35
|
+
The `color` and `spectrum` objects automatically change based on the `activeColorScheme`.
|
|
36
|
+
|
|
37
|
+
[See the `useTheme` docs here →](/hooks/useTheme)
|
|
38
|
+
|
|
39
|
+
```jsx
|
|
40
|
+
const theme = useTheme();
|
|
41
|
+
theme.activeColorScheme; // "light" or "dark"
|
|
42
|
+
theme.spectrum; // Resolves to lightSpectrum or darkSpectrum, depends on activeColorScheme
|
|
43
|
+
theme.color; // Resolves to lightColor or darkColor, depends on activeColorScheme
|
|
44
|
+
theme.color.bgPrimary; // "rgb(0,82,255)" or "rgb(87,139,250)", depends on activeColorScheme
|
|
45
|
+
theme.space[2]; // 16
|
|
46
|
+
theme.borderRadius[200]; // 8
|
|
47
|
+
theme.fontSize.display3; // "2.5rem"
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
:::tip
|
|
51
|
+
For best performance, prefer to use CSS Variables instead of the `useTheme` hook whenever possible.
|
|
52
|
+
:::
|
|
53
|
+
|
|
54
|
+
#### ThemeProvider CSS Variables
|
|
55
|
+
|
|
56
|
+
CSS Variables are created for every value in the theme.
|
|
57
|
+
|
|
58
|
+
For best performance, prefer to use CSS Variables instead of the `useTheme` hook whenever possible.
|
|
59
|
+
|
|
60
|
+
```jsx
|
|
61
|
+
const theme = useTheme();
|
|
62
|
+
theme.color.bgPrimary; // --color-bgPrimary
|
|
63
|
+
theme.lightColor.bgPrimary; // --lightColor-bgPrimary
|
|
64
|
+
theme.darkColor.bgPrimary; // --darkColor-bgPrimary
|
|
65
|
+
theme.spectrum.blue10; // --blue10
|
|
66
|
+
theme.lightSpectrum.blue10; // --light-blue10
|
|
67
|
+
theme.darkSpectrum.blue10; // --dark-blue10
|
|
68
|
+
theme.space[2]; // --space-2
|
|
69
|
+
theme.space[0.25]; // --space-0_25
|
|
70
|
+
theme.borderRadius[400]; // --borderRadius-400
|
|
71
|
+
theme.fontSize.body; // --fontSize-body
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
You can see all the CSS Variables for the `defaultTheme` below.
|
|
75
|
+
|
|
76
|
+
<JSONCodeBlock json={createThemeCssVars(defaultTheme)} />
|
|
77
|
+
|
|
78
|
+
#### ThemeProvider classnames
|
|
79
|
+
|
|
80
|
+
The ThemeProvider renders with CSS classnames based on the `activeColorScheme` and the theme's `id`.
|
|
81
|
+
|
|
82
|
+
This allows you to style components based on the `activeColorScheme` or the theme's `id`.
|
|
83
|
+
|
|
84
|
+
```jsx
|
|
85
|
+
// Renders with a .cds-default class and a .light class
|
|
86
|
+
<ThemeProvider theme={defaultTheme} activeColorScheme="light" />
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
#### Nested themes
|
|
90
|
+
|
|
91
|
+
ThemeProviders can be nested to create theme overrides for specific sections.
|
|
92
|
+
|
|
93
|
+
```jsx
|
|
94
|
+
<ThemeProvider theme={defaultTheme} activeColorScheme="light">
|
|
95
|
+
{/* Default theme in light color scheme */}
|
|
96
|
+
|
|
97
|
+
<ThemeProvider theme={customTheme} activeColorScheme="dark">
|
|
98
|
+
{/* Custom theme in dark color scheme */}
|
|
99
|
+
</ThemeProvider>
|
|
100
|
+
</ThemeProvider>
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
When nesting, you may want to override specific color values from the current theme. Overrides must be conditionally applied because we don't enforce that a theme has both light and dark colors defined.
|
|
104
|
+
|
|
105
|
+
```jsx
|
|
106
|
+
// Override parts of the parent theme
|
|
107
|
+
const theme = useTheme();
|
|
108
|
+
const customTheme = {
|
|
109
|
+
...theme,
|
|
110
|
+
...(theme.lightColor &&
|
|
111
|
+
theme.lightSpectrum && {
|
|
112
|
+
lightColor: {
|
|
113
|
+
...theme.lightColor,
|
|
114
|
+
bg: `rgb(${theme.lightSpectrum.orange50})`,
|
|
115
|
+
bgPrimary: `rgb(${theme.lightSpectrum.red20})`,
|
|
116
|
+
bgSecondary: `rgb(${theme.lightSpectrum.blue50})`,
|
|
117
|
+
},
|
|
118
|
+
}),
|
|
119
|
+
...(theme.darkColor &&
|
|
120
|
+
theme.darkSpectrum && {
|
|
121
|
+
darkColor: {
|
|
122
|
+
...theme.darkColor,
|
|
123
|
+
bg: `rgb(${theme.darkSpectrum.orange50})`,
|
|
124
|
+
bgPrimary: `rgb(${theme.darkSpectrum.red20})`,
|
|
125
|
+
bgSecondary: `rgb(${theme.darkSpectrum.blue50})`,
|
|
126
|
+
},
|
|
127
|
+
}),
|
|
128
|
+
} as const satisfies Theme;
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
#### Theme inheritence
|
|
132
|
+
|
|
133
|
+
Nested ThemeProviders do not automatically inherit the theme from their parent provider. You can manually inherit the theme with the `useTheme` hook.
|
|
134
|
+
|
|
135
|
+
```jsx
|
|
136
|
+
const Example = () => {
|
|
137
|
+
// Pass the parent theme down to another ThemeProvider
|
|
138
|
+
const theme = useTheme();
|
|
139
|
+
return (
|
|
140
|
+
<ThemeProvider theme={theme} activeColorScheme={theme.activeColorScheme}>
|
|
141
|
+
{children}
|
|
142
|
+
</ThemeProvider>
|
|
143
|
+
);
|
|
144
|
+
};
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
#### InvertedThemeProvider component
|
|
148
|
+
|
|
149
|
+
The InvertedThemeProvider automatically inherits from its parent theme and flips the `activeColorScheme` to the opposite value.
|
|
150
|
+
|
|
151
|
+
```jsx
|
|
152
|
+
<ThemeProvider theme={defaultTheme} activeColorScheme="light">
|
|
153
|
+
{/* Default theme in light color scheme */}
|
|
154
|
+
|
|
155
|
+
<InvertedThemeProvider>
|
|
156
|
+
{/* Default theme in inverse (dark) color scheme */}
|
|
157
|
+
</InvertedThemeProvider>
|
|
158
|
+
</ThemeProvider>
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### `ThemeConfig` vs `Theme` type
|
|
162
|
+
|
|
163
|
+
Use the `ThemeConfig` type when creating a theme, or when passing the `theme` prop to the ThemeProvider.
|
|
164
|
+
|
|
165
|
+
Use the `Theme` type for the return value of the `useTheme` hook. The `Theme` type includes all the properties of `ThemeConfig` - plus the `activeColorScheme`, `color`, and `spectrum` properties.
|
|
166
|
+
|
|
167
|
+
[See the `ThemeConfig` type definition here →](https://github.com/coinbase/cds/blob/master/packages/web/src/core/theme.ts#L4)
|
|
168
|
+
|
|
169
|
+
[See the `Theme` type definition here →](https://github.com/coinbase/cds/blob/master/packages/web/src/core/theme.ts#L43)
|
|
170
|
+
|
|
171
|
+
:::tip
|
|
172
|
+
Although the `Theme` type includes extra properties, you can still pass the `useTheme` return value directly to the ThemeProvider `theme` prop as shown in the [theme inheritence section.](#theme-inheritence)
|
|
173
|
+
:::
|
|
174
|
+
|
|
175
|
+
### `spectrum` vs `color` values
|
|
176
|
+
|
|
177
|
+
The `spectrum` variables are partial `"r,g,b"` strings while `color` variables are complete CSS color values.
|
|
178
|
+
|
|
179
|
+
Both `color` and `spectrum` behave inversely in light and dark mode.
|
|
180
|
+
|
|
181
|
+
For example with the `defaultTheme` config, `spectrum.gray0` is white in light mode and black in dark mode. We use `spectrum` variables to define `color` variables, so this same behavior extends to `color`.
|
|
182
|
+
|
|
183
|
+
The `color` variables have semantic names which describe their application in the UI. You should prefer to use `color` variables instead of `spectrum` variables when styling UI.
|
|
184
|
+
|
|
185
|
+
```jsx
|
|
186
|
+
const theme = useTheme();
|
|
187
|
+
theme.lightSpectrum.gray0; // "255,255,255"
|
|
188
|
+
theme.darkSpectrum.gray0; // "10,11,13"
|
|
189
|
+
theme.spectrum.gray100; // "255,255,255" or "10,11,13", depends on activeColorScheme
|
|
190
|
+
theme.lightColor.bg; // "rgb(255,255,255)"
|
|
191
|
+
theme.darkColor.bg; // "rgb(10,11,13)"
|
|
192
|
+
theme.color.bg; // "rgb(255,255,255)" or "rgb(10,11,13)", depends on activeColorScheme
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
:::tip
|
|
196
|
+
Prefer to use semantic `color` variables for UI styles. Direct usage of `spectrum` values should be a rare exception.
|
|
197
|
+
:::
|
|
198
|
+
|
|
199
|
+
### Creating a theme
|
|
200
|
+
#### Defining colors
|
|
201
|
+
|
|
202
|
+
We recommend defining `lightSpectrum` and `darkSpectrum` as separate objects. This makes it easier to reference them when defining the `lightColor` and `darkColor` values.
|
|
203
|
+
|
|
204
|
+
The `lightColor` and `darkColor` values must be raw color strings (hex, rgba, hsl, etc), they cannot contain CSS Variables or CSS functions.
|
|
205
|
+
|
|
206
|
+
#### The `space` scale
|
|
207
|
+
|
|
208
|
+
CDS expects that the theme `space` values will be multiples of some base number.
|
|
209
|
+
|
|
210
|
+
For example, the `defaultTheme` uses `8px` as the base number:
|
|
211
|
+
|
|
212
|
+
```jsx
|
|
213
|
+
theme.space[0]; // 0
|
|
214
|
+
theme.space[0.25]; // 2
|
|
215
|
+
theme.space[0.5]; // 4
|
|
216
|
+
theme.space[1]; // 8
|
|
217
|
+
theme.space[1.5]; // 12
|
|
218
|
+
theme.space[2]; // 16
|
|
219
|
+
theme.space[3]; // 24
|
|
220
|
+
// etc.
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
While it is possible to customize the `space` values in any way, deviating from this expectation may produce broken styles.
|
|
224
|
+
|
|
225
|
+
#### Example new theme
|
|
226
|
+
|
|
227
|
+
In this example we'll start with the `defaultTheme` and customize a couple values.
|
|
228
|
+
|
|
229
|
+
```tsx
|
|
230
|
+
import type { ThemeConfig } from '@coinbase/cds-web/core/theme';
|
|
231
|
+
import { ThemeProvider } from '@coinbase/cds-web/system/ThemeProvider';
|
|
232
|
+
import { defaultTheme } from '@coinbase/cds-web/themes/defaultTheme';
|
|
233
|
+
|
|
234
|
+
// Define lightSpectrum and darkSpectrum as separate objects
|
|
235
|
+
const lightSpectrum = {
|
|
236
|
+
...defaultTheme.lightSpectrum,
|
|
237
|
+
blue60: '8,90,255',
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
const darkSpectrum = {
|
|
241
|
+
...defaultTheme.darkSpectrum,
|
|
242
|
+
blue60: '65,125,245',
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
// Use lightSpectrum and darkSpectrum to define the lightColor and darkColor values
|
|
246
|
+
const myTheme = {
|
|
247
|
+
...defaultTheme,
|
|
248
|
+
id: 'my-custom-theme',
|
|
249
|
+
lightSpectrum,
|
|
250
|
+
darkSpectrum,
|
|
251
|
+
lightColor: {
|
|
252
|
+
...defaultTheme.lightColor,
|
|
253
|
+
bgPrimary: `rgb(${lightSpectrum.red60})`,
|
|
254
|
+
},
|
|
255
|
+
darkColor: {
|
|
256
|
+
...defaultTheme.darkColor,
|
|
257
|
+
bgPrimary: `rgb(${darkSpectrum.red60})`,
|
|
258
|
+
},
|
|
259
|
+
} as const satisfies ThemeConfig;
|
|
260
|
+
|
|
261
|
+
const App = () => (
|
|
262
|
+
<ThemeProvider theme={myTheme} activeColorScheme="light">
|
|
263
|
+
{/* Your app components */}
|
|
264
|
+
</ThemeProvider>
|
|
265
|
+
);
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
:::tip
|
|
269
|
+
Use the `ThemeConfig` type with TypeScript's `satisfies` keyword to enforce type safety for your theme.
|
|
270
|
+
:::
|
|
271
|
+
|
|
272
|
+
### `defaultTheme` config
|
|
273
|
+
|
|
274
|
+
The `defaultTheme` is a good example of a complete `ThemeConfig`. You can use it as an example when developing your own themes.
|
|
275
|
+
|
|
276
|
+
[See the `defaultTheme` source code here →](https://github.com/coinbase/cds/blob/master/packages/web/src/themes/defaultTheme.ts)
|
|
277
|
+
|
|
278
|
+
<JSONCodeBlock json={defaultTheme} />
|
|
279
|
+
|
|
280
|
+
### `ThemeVars` namespace
|
|
281
|
+
|
|
282
|
+
The `ThemeVars` namespace contains type definitions for all themeable variable names in CDS. It includes any custom theme variables added in `ThemeVarsExtended`.
|
|
283
|
+
|
|
284
|
+
```tsx
|
|
285
|
+
import type { ThemeVars } from '@coinbase/cds-common/core/theme';
|
|
286
|
+
|
|
287
|
+
// All theme variables are accessible through ThemeVars
|
|
288
|
+
ThemeVars.Color; // 'fg' | 'bg' | 'bgPrimary' | 'bgSecondary' | ...
|
|
289
|
+
ThemeVars.SpectrumColor; // 'blue60' | 'red40' | 'gray100' | ...
|
|
290
|
+
ThemeVars.SpectrumHue; // 'blue' | 'green' | 'orange' | 'gray' | ...
|
|
291
|
+
ThemeVars.SpectrumHueStep; // 0 | 5 | 10 | 20 | ...
|
|
292
|
+
ThemeVars.Space; // 0 | 0.25 | 0.5 | 1 | 2 | ...
|
|
293
|
+
ThemeVars.BorderWidth; // 0 | 100 | 200 | ...
|
|
294
|
+
ThemeVars.BorderRadius; // 0 | 100 | 200 | ...
|
|
295
|
+
ThemeVars.Font; // 'display1' | 'title1' | 'body' | ...
|
|
296
|
+
ThemeVars.FontFamily; // 'display1' | 'title1' | 'body' | ...
|
|
297
|
+
ThemeVars.FontSize; // 'display1' | 'title1' | 'body' | ...
|
|
298
|
+
ThemeVars.FontWeight; // 'display1' | 'title1' | 'body' | ...
|
|
299
|
+
ThemeVars.LineHeight; // 'display1' | 'title1' | 'body' | ...
|
|
300
|
+
ThemeVars.TextTransform; // 'display1' | 'title1' | 'body' | ...
|
|
301
|
+
ThemeVars.IconSize; // 'xs' | 's' | 'm' | 'l'
|
|
302
|
+
ThemeVars.AvatarSize; // 's' | 'm' | 'l' | 'xl' | ...
|
|
303
|
+
ThemeVars.ControlSize; // 'checkboxSize' | 'radioSize' | ...
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
### Extending the theme
|
|
307
|
+
|
|
308
|
+
You can extend the theme by adding custom theme variables. In this example we'll show how to add a custom color variable, but you can extend other types of `ThemeVars` as well.
|
|
309
|
+
|
|
310
|
+
Start by overriding interfaces in the `ThemeVarsExtended` namespace to add new theme variables. Only the key names are used, the `void` type is just a necessary placeholder.
|
|
311
|
+
|
|
312
|
+
```jsx
|
|
313
|
+
declare module '@coinbase/cds-common/core/theme' {
|
|
314
|
+
export namespace ThemeVarsExtended {
|
|
315
|
+
export interface Color {
|
|
316
|
+
myCustomColor: void;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
This adds `myCustomColor` to `ThemeVars.Color` - which enforces that this variable must be defined in your theme to satisfy the `ThemeConfig` type, and allows this variable name to be passed to the `StyleProps` API anywhere that theme colors are accepted.
|
|
323
|
+
|
|
324
|
+
Now that `myCustomColor` is defined in your theme the `useTheme` hook will include it in the return value, and the ThemeProvider will create CSS Variables for this value just like every other `ThemeVars.Color`.
|
|
325
|
+
|
|
326
|
+
Next define the corresponding static classnames for your new theme variables via the `initializeCDS` function `extendStyleProps` options. These static classnames should use the CSS Variable that will be created by the ThemeProvider.
|
|
327
|
+
|
|
328
|
+
:::tip
|
|
329
|
+
Call the `initializeCDS` function only once in your app entry point, before your app renders.
|
|
330
|
+
:::
|
|
331
|
+
|
|
332
|
+
Web StyleProps support responsive syntax, so you must provide 4 unique classnames per style prop: one each for `base`, `phone`, `tablet`, and `desktop`. Use the [CDS media object](/getting-started/styling/#responsive-styles) to use the same responsive breakpoints as CDS.
|
|
333
|
+
|
|
334
|
+
```jsx
|
|
335
|
+
import { initializeCDS } from '@coinbase/cds-web/styles/config';
|
|
336
|
+
|
|
337
|
+
initializeCDS({
|
|
338
|
+
extendStyleProps: {
|
|
339
|
+
// Specify the ThemeVars interface name that you extended
|
|
340
|
+
Color: {
|
|
341
|
+
// Specify the new theme variable name that you added
|
|
342
|
+
myCustomColor: {
|
|
343
|
+
// Configure the necessary static classNames for the new theme variable
|
|
344
|
+
background: {
|
|
345
|
+
base: 'background-myCustomColor',
|
|
346
|
+
phone: 'background-myCustomColor-phone',
|
|
347
|
+
tablet: 'background-myCustomColor-tablet',
|
|
348
|
+
desktop: 'background-myCustomColor-desktop',
|
|
349
|
+
},
|
|
350
|
+
color: {
|
|
351
|
+
base: 'color-myCustomColor',
|
|
352
|
+
phone: 'color-myCustomColor-phone',
|
|
353
|
+
tablet: 'color-myCustomColor-tablet',
|
|
354
|
+
desktop: 'color-myCustomColor-desktop',
|
|
355
|
+
},
|
|
356
|
+
borderColor: {
|
|
357
|
+
base: 'borderColor-myCustomColor',
|
|
358
|
+
phone: 'borderColor-myCustomColor-phone',
|
|
359
|
+
tablet: 'borderColor-myCustomColor-tablet',
|
|
360
|
+
desktop: 'borderColor-myCustomColor-desktop',
|
|
361
|
+
},
|
|
362
|
+
},
|
|
363
|
+
},
|
|
364
|
+
},
|
|
365
|
+
});
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
With the classnames defined as follows:
|
|
369
|
+
|
|
370
|
+
```css
|
|
371
|
+
.background-myCustomColor {
|
|
372
|
+
background-color: var(--color-myCustomColor);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
.background-myCustomColor-phone {
|
|
376
|
+
@media (max-width: 767px) {
|
|
377
|
+
background-color: var(--color-myCustomColor);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
.background-myCustomColor-tablet {
|
|
382
|
+
@media (min-width: 768px) and (max-width: 1279px) {
|
|
383
|
+
background-color: var(--color-myCustomColor);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
.background-myCustomColor-desktop {
|
|
388
|
+
@media (min-width: 1280px) {
|
|
389
|
+
background-color: var(--color-myCustomColor);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/* etc for color and borderColor */
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
Or, using CSS-in-JS:
|
|
397
|
+
|
|
398
|
+
```tsx
|
|
399
|
+
import { css } from '@linaria/core';
|
|
400
|
+
import { media } from '@coinbase/cds-web/styles/media';
|
|
401
|
+
|
|
402
|
+
const myCustomColor = {
|
|
403
|
+
background: {
|
|
404
|
+
base: css`
|
|
405
|
+
background-color: var(--color-myCustomColor);
|
|
406
|
+
`,
|
|
407
|
+
phone: css`
|
|
408
|
+
@media ${media.phone} {
|
|
409
|
+
background-color: var(--color-myCustomColor);
|
|
410
|
+
}
|
|
411
|
+
`,
|
|
412
|
+
tablet: css`
|
|
413
|
+
@media ${media.tablet} {
|
|
414
|
+
background-color: var(--color-myCustomColor);
|
|
415
|
+
}
|
|
416
|
+
`,
|
|
417
|
+
desktop: css`
|
|
418
|
+
@media ${media.desktop} {
|
|
419
|
+
background-color: var(--color-myCustomColor);
|
|
420
|
+
}
|
|
421
|
+
`,
|
|
422
|
+
},
|
|
423
|
+
// etc for color and borderColor
|
|
424
|
+
};
|
|
425
|
+
```
|
|
426
|
+
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
1
|
# useBreakpoints
|
|
2
|
+
|
|
3
3
|
Returns an object with a boolean for each breakpoint of the window. Useful for conditionally rendering components or component trees based on the current window breakpoint.
|
|
4
4
|
|
|
5
5
|
## Import
|
|
@@ -8,6 +8,35 @@ Returns an object with a boolean for each breakpoint of the window. Useful for c
|
|
|
8
8
|
import { useBreakpoints } from '@coinbase/cds-web/hooks/useBreakpoints'
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
+
## API
|
|
12
|
+
|
|
13
|
+
import { media } from '@coinbase/cds-web/styles/media';
|
|
14
|
+
|
|
15
|
+
### Parameters
|
|
16
|
+
|
|
17
|
+
The `useBreakpoints` hook does not accept any parameters. It must be used within a MediaQueryProvider component.
|
|
18
|
+
|
|
19
|
+
### Returns
|
|
20
|
+
|
|
21
|
+
Returns an object with a boolean for each breakpoint of the window.
|
|
22
|
+
|
|
23
|
+
- `isPhone`: {media.phone}
|
|
24
|
+
- `isPhonePortrait`: {media.phonePortrait}
|
|
25
|
+
- `isPhoneLandscape`: {media.phoneLandscape}
|
|
26
|
+
- `isTablet`: {media.tablet}
|
|
27
|
+
- `isTabletPortrait`: {media.tabletPortrait}
|
|
28
|
+
- `isTabletLandscape`: {media.tabletLandscape}
|
|
29
|
+
- `isDesktop`: {media.desktop}
|
|
30
|
+
- `isDesktopSmall`: {media.desktopSmall}
|
|
31
|
+
- `isDesktopLarge`: {media.desktopLarge}
|
|
32
|
+
- `isExtraWide`: {media.extraWide}
|
|
33
|
+
|
|
34
|
+
[See how breakpoints are defined in the `media` object →](https://github.com/coinbase/cds/blob/master/packages/web/src/styles/media.ts)
|
|
35
|
+
|
|
36
|
+
:::tip
|
|
37
|
+
Multiple breakpoint values can be true at the same time.
|
|
38
|
+
:::
|
|
39
|
+
|
|
11
40
|
## Examples
|
|
12
41
|
|
|
13
42
|
import { JSONCodeBlock } from '@site/src/components/page/JSONCodeBlock';
|
|
@@ -30,4 +59,3 @@ Do not use `useBreakpoints` for responsive styles. Use CSS media queries or [the
|
|
|
30
59
|
};
|
|
31
60
|
```
|
|
32
61
|
|
|
33
|
-
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
1
|
# useDimensions
|
|
2
|
+
|
|
3
3
|
Measures an element's dimensions using ResizeObserver.
|
|
4
4
|
|
|
5
5
|
## Import
|
|
@@ -8,6 +8,66 @@ Measures an element's dimensions using ResizeObserver.
|
|
|
8
8
|
import { useDimensions } from '@coinbase/cds-web/hooks/useDimensions'
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
+
## API
|
|
12
|
+
|
|
13
|
+
### Parameters
|
|
14
|
+
|
|
15
|
+
The hook accepts an optional configuration object with the following properties:
|
|
16
|
+
|
|
17
|
+
#### Options
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
type Options<T extends HTMLElement> = {
|
|
21
|
+
ref?: RefObject<T> | null;
|
|
22
|
+
useBorderBoxSize?: boolean;
|
|
23
|
+
breakpoints?: Record<string, number>;
|
|
24
|
+
updateOnBreakpointChange?: boolean;
|
|
25
|
+
shouldUpdate?: (state: State) => boolean;
|
|
26
|
+
onResize?: (event: Event<T>) => void;
|
|
27
|
+
polyfill?: any;
|
|
28
|
+
};
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
- `ref` (optional): A React ref to observe. If not provided, the hook will create one.
|
|
32
|
+
- `useBorderBoxSize` (optional): Whether to use border-box size measurement instead of content-box. Defaults to `false`.
|
|
33
|
+
- `breakpoints` (optional): An object mapping breakpoint names to width values.
|
|
34
|
+
- `updateOnBreakpointChange` (optional): Whether to update state only when breakpoint changes. Defaults to `false`.
|
|
35
|
+
- `shouldUpdate` (optional): A function to conditionally control state updates.
|
|
36
|
+
- `onResize` (optional): Callback fired when the observed element resizes.
|
|
37
|
+
- `polyfill` (optional): A ResizeObserver polyfill for unsupported browsers.
|
|
38
|
+
|
|
39
|
+
### Returns
|
|
40
|
+
|
|
41
|
+
Returns an object with the following properties:
|
|
42
|
+
|
|
43
|
+
```tsx
|
|
44
|
+
type Return<T> = {
|
|
45
|
+
ref: RefObject<T>;
|
|
46
|
+
currentBreakpoint: string;
|
|
47
|
+
width: number;
|
|
48
|
+
height: number;
|
|
49
|
+
x: number;
|
|
50
|
+
y: number;
|
|
51
|
+
entry?: ResizeObserverEntry;
|
|
52
|
+
observe: (element: T | null) => void;
|
|
53
|
+
unobserve: () => void;
|
|
54
|
+
};
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
- `ref`: React ref to attach to the element to observe.
|
|
58
|
+
- `currentBreakpoint`: Current active breakpoint based on element width. Empty if no breakpoints defined.
|
|
59
|
+
- `width`: Width of the observed element in pixels.
|
|
60
|
+
- `height`: Height of the observed element in pixels.
|
|
61
|
+
- `x`: X-coordinate of the element relative to the viewport.
|
|
62
|
+
- `y`: Y-coordinate of the element relative to the viewport.
|
|
63
|
+
- `entry`: Raw ResizeObserver entry object.
|
|
64
|
+
- `observe`: Function to start observing an element.
|
|
65
|
+
- `unobserve`: Function to stop observing the current element.
|
|
66
|
+
|
|
67
|
+
:::tip
|
|
68
|
+
This hook uses the ResizeObserver API to track element dimensions. A polyfill may be required for older browsers.
|
|
69
|
+
:::
|
|
70
|
+
|
|
11
71
|
## Examples
|
|
12
72
|
|
|
13
73
|
### Basic usage
|
|
@@ -52,4 +112,3 @@ function Example() {
|
|
|
52
112
|
}
|
|
53
113
|
```
|
|
54
114
|
|
|
55
|
-
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# useEventHandler
|
|
2
|
+
|
|
3
|
+
Creates event handlers for components based on the EventHandlerProvider configuration. It enables centralized event handling and analytics tracking by mapping component actions to configured handlers.
|
|
4
|
+
|
|
5
|
+
## Import
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import { useEventHandler } from '@coinbase/cds-common/hooks/useEventHandler'
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## API
|
|
12
|
+
|
|
13
|
+
:::warning
|
|
14
|
+
Must be used within an EventHandlerProvider. The provider's configuration determines which events are handled and how they are processed. The provider config should include:
|
|
15
|
+
|
|
16
|
+
- `actionMapping?: Record<string, string>` - Optional mapping of CDS actions to handler actions
|
|
17
|
+
- `handlers?: Record<EventHandlerComponent, Record<string, (eventData: EventCallbackProps) => void>>` - Event handlers for components
|
|
18
|
+
|
|
19
|
+
:::
|
|
20
|
+
|
|
21
|
+
### Parameters
|
|
22
|
+
|
|
23
|
+
```tsx
|
|
24
|
+
type EventDataEntryTypes = string | number | boolean | null | undefined;
|
|
25
|
+
type EventDataEntry = EventDataEntryTypes | EventDataEntryTypes[];
|
|
26
|
+
type RecursiveMapType<T> = T | Record<string, T>;
|
|
27
|
+
|
|
28
|
+
type EventHandlerComponent = 'Button';
|
|
29
|
+
type EventHandlerAction = string;
|
|
30
|
+
type EventCustomData = Record<string, RecursiveMapType<EventDataEntry>>;
|
|
31
|
+
|
|
32
|
+
type EventHandlerCustomConfig = {
|
|
33
|
+
componentName: string;
|
|
34
|
+
actions: EventHandlerAction[];
|
|
35
|
+
data?: EventCustomData;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
function useEventHandler(
|
|
39
|
+
component: EventHandlerComponent,
|
|
40
|
+
action: EventHandlerAction,
|
|
41
|
+
eventConfig?: EventHandlerCustomConfig,
|
|
42
|
+
analyticsId?: string,
|
|
43
|
+
): () => void;
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
- `component`: The component type to handle events for (currently supports 'Button')
|
|
47
|
+
- `action`: The action type to handle (e.g., 'onClick', 'onHover'). This is a string type that can be mapped to custom handler actions.
|
|
48
|
+
- `eventConfig` (optional): Configuration object for the event:
|
|
49
|
+
- `componentName`: Name of the component for tracking purposes
|
|
50
|
+
- `actions`: List of actions to track
|
|
51
|
+
- `data`: Additional data to pass to the handler
|
|
52
|
+
- `analyticsId` (optional): A unique identifier for analytics tracking that takes precedence over eventConfig
|
|
53
|
+
|
|
54
|
+
### Returns
|
|
55
|
+
|
|
56
|
+
```tsx
|
|
57
|
+
type EventCallbackProps = {
|
|
58
|
+
componentName?: string;
|
|
59
|
+
analyticsId?: string;
|
|
60
|
+
data?: EventCustomData;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
type Return = () => void;
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Returns a callback function that when invoked will trigger the event handling. The function:
|
|
67
|
+
|
|
68
|
+
- Executes the configured handler for the component and action when called
|
|
69
|
+
- Maps CDS actions to custom handler actions if `actionMapping` is provided in the EventHandlerProvider
|
|
70
|
+
- Returns a no-op function if:
|
|
71
|
+
- No handlers are configured
|
|
72
|
+
- No eventConfig actions are provided and no analyticsId is set
|
|
73
|
+
- The handler for the component doesn't exist
|
|
74
|
+
- The action is not listed in eventConfig actions (when using eventConfig)
|
|
75
|
+
- The params object is empty
|
|
76
|
+
|
|
77
|
+
## Examples
|
|
78
|
+
|
|
79
|
+
### Usage
|
|
80
|
+
|
|
81
|
+
```tsx live
|
|
82
|
+
function ExampleWithProvider() {
|
|
83
|
+
const { show: showToast } = useToast();
|
|
84
|
+
|
|
85
|
+
// Example provider configuration with action mapping
|
|
86
|
+
const config = {
|
|
87
|
+
actionMapping: {
|
|
88
|
+
onClick: 'click', // Maps CDS onClick to custom 'click' handler
|
|
89
|
+
},
|
|
90
|
+
handlers: {
|
|
91
|
+
Button: {
|
|
92
|
+
click: ({ componentName, data }) => {
|
|
93
|
+
showToast(`Button ${componentName} clicked with data: ${JSON.stringify(data)}`);
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
function ButtonExample() {
|
|
100
|
+
const eventConfig = {
|
|
101
|
+
actions: ['click'], // Use mapped action name
|
|
102
|
+
componentName: 'mapped_button',
|
|
103
|
+
data: {
|
|
104
|
+
source: 'action_mapping_example',
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const handleButtonClick = useEventHandler('Button', 'onClick', eventConfig);
|
|
109
|
+
|
|
110
|
+
return <Button onClick={handleButtonClick}>Click for Mapped Action</Button>;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return (
|
|
114
|
+
<EventHandlerProvider config={config}>
|
|
115
|
+
<ButtonExample />
|
|
116
|
+
</EventHandlerProvider>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|