@coinbase/cds-mcp-server 8.17.2 → 8.17.4
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 +188 -0
- package/mcp-docs/mobile/components/AccordionItem.txt +29 -0
- package/mcp-docs/mobile/components/Alert.txt +155 -0
- package/mcp-docs/mobile/components/AreaChart.txt +265 -0
- package/mcp-docs/mobile/components/Avatar.txt +195 -0
- package/mcp-docs/mobile/components/AvatarButton.txt +225 -0
- package/mcp-docs/mobile/components/Banner.txt +221 -0
- package/mcp-docs/mobile/components/BarChart.txt +815 -0
- package/mcp-docs/mobile/components/Box.txt +173 -0
- package/mcp-docs/mobile/components/BrowserBar.txt +146 -0
- package/mcp-docs/mobile/components/Button.txt +198 -0
- package/mcp-docs/mobile/components/ButtonGroup.txt +79 -0
- package/mcp-docs/mobile/components/Carousel.txt +1083 -0
- package/mcp-docs/mobile/components/CartesianChart.txt +825 -0
- package/mcp-docs/mobile/components/CellMedia.txt +70 -0
- package/mcp-docs/mobile/components/Checkbox.txt +245 -0
- package/mcp-docs/mobile/components/CheckboxCell.txt +201 -0
- package/mcp-docs/mobile/components/CheckboxGroup.txt +284 -0
- package/mcp-docs/mobile/components/Chip.txt +194 -0
- package/mcp-docs/mobile/components/Coachmark.txt +157 -0
- package/mcp-docs/mobile/components/Collapsible.txt +104 -0
- package/mcp-docs/mobile/components/ContainedAssetCard.txt +134 -0
- package/mcp-docs/mobile/components/ContentCard.txt +365 -0
- package/mcp-docs/mobile/components/ContentCardBody.txt +135 -0
- package/mcp-docs/mobile/components/ContentCardFooter.txt +127 -0
- package/mcp-docs/mobile/components/ContentCardHeader.txt +145 -0
- package/mcp-docs/mobile/components/ContentCell.txt +226 -0
- package/mcp-docs/mobile/components/ControlGroup.txt +443 -0
- package/mcp-docs/mobile/components/DatePicker.txt +496 -0
- package/mcp-docs/mobile/components/Divider.txt +138 -0
- package/mcp-docs/mobile/components/DotCount.txt +145 -0
- package/mcp-docs/mobile/components/DotStatusColor.txt +58 -0
- package/mcp-docs/mobile/components/DotSymbol.txt +134 -0
- package/mcp-docs/mobile/components/Fallback.txt +157 -0
- package/mcp-docs/mobile/components/FloatingAssetCard.txt +155 -0
- package/mcp-docs/mobile/components/HStack.txt +234 -0
- package/mcp-docs/mobile/components/HeroSquare.txt +47 -0
- package/mcp-docs/mobile/components/Icon.txt +51 -0
- package/mcp-docs/mobile/components/IconButton.txt +268 -0
- package/mcp-docs/mobile/components/InputChip.txt +187 -0
- package/mcp-docs/mobile/components/Interactable.txt +186 -0
- package/mcp-docs/mobile/components/LineChart.txt +1324 -0
- package/mcp-docs/mobile/components/Link.txt +291 -0
- package/mcp-docs/mobile/components/ListCell.txt +412 -0
- package/mcp-docs/mobile/components/LogoMark.txt +84 -0
- package/mcp-docs/mobile/components/LogoWordMark.txt +93 -0
- package/mcp-docs/mobile/components/Lottie.txt +138 -0
- package/mcp-docs/mobile/components/LottieStatusAnimation.txt +46 -0
- package/mcp-docs/mobile/components/Modal.txt +83 -0
- package/mcp-docs/mobile/components/ModalBody.txt +33 -0
- package/mcp-docs/mobile/components/ModalFooter.txt +24 -0
- package/mcp-docs/mobile/components/ModalHeader.txt +27 -0
- package/mcp-docs/mobile/components/MultiContentModule.txt +379 -0
- package/mcp-docs/mobile/components/NavigationTitle.txt +131 -0
- package/mcp-docs/mobile/components/NavigationTitleSelect.txt +141 -0
- package/mcp-docs/mobile/components/NudgeCard.txt +89 -0
- package/mcp-docs/mobile/components/Numpad.txt +340 -0
- package/mcp-docs/mobile/components/Overlay.txt +151 -0
- package/mcp-docs/mobile/components/PageFooter.txt +160 -0
- package/mcp-docs/mobile/components/PageHeader.txt +185 -0
- package/mcp-docs/mobile/components/PeriodSelector.txt +407 -0
- package/mcp-docs/mobile/components/Pictogram.txt +47 -0
- package/mcp-docs/mobile/components/Point.txt +204 -0
- package/mcp-docs/mobile/components/PortalProvider.txt +78 -0
- package/mcp-docs/mobile/components/Pressable.txt +210 -0
- package/mcp-docs/mobile/components/ProgressBar.txt +129 -0
- package/mcp-docs/mobile/components/ProgressBarWithFixedLabels.txt +160 -0
- package/mcp-docs/mobile/components/ProgressBarWithFloatLabel.txt +137 -0
- package/mcp-docs/mobile/components/ProgressCircle.txt +236 -0
- package/mcp-docs/mobile/components/Radio.txt +241 -0
- package/mcp-docs/mobile/components/RadioCell.txt +201 -0
- package/mcp-docs/mobile/components/RadioGroup.txt +281 -0
- package/mcp-docs/mobile/components/ReferenceLine.txt +152 -0
- package/mcp-docs/mobile/components/RemoteImage.txt +105 -0
- package/mcp-docs/mobile/components/RemoteImageGroup.txt +60 -0
- package/mcp-docs/mobile/components/RollingNumber.txt +788 -0
- package/mcp-docs/mobile/components/Scrubber.txt +203 -0
- package/mcp-docs/mobile/components/SearchInput.txt +191 -0
- package/mcp-docs/mobile/components/SectionHeader.txt +204 -0
- package/mcp-docs/mobile/components/SegmentedTabs.txt +315 -0
- package/mcp-docs/mobile/components/Select.txt +211 -0
- package/mcp-docs/mobile/components/SelectChip.txt +323 -0
- package/mcp-docs/mobile/components/SelectOption.txt +84 -0
- package/mcp-docs/mobile/components/SlideButton.txt +330 -0
- package/mcp-docs/mobile/components/Spacer.txt +83 -0
- package/mcp-docs/mobile/components/Sparkline.txt +122 -0
- package/mcp-docs/mobile/components/SparklineGradient.txt +106 -0
- package/mcp-docs/mobile/components/SparklineInteractive.txt +156 -0
- package/mcp-docs/mobile/components/SparklineInteractiveHeader.txt +72 -0
- package/mcp-docs/mobile/components/Spinner.txt +48 -0
- package/mcp-docs/mobile/components/SpotIcon.txt +47 -0
- package/mcp-docs/mobile/components/SpotRectangle.txt +47 -0
- package/mcp-docs/mobile/components/SpotSquare.txt +47 -0
- package/mcp-docs/mobile/components/Stepper.txt +527 -0
- package/mcp-docs/mobile/components/SubBrandLogoMark.txt +125 -0
- package/mcp-docs/mobile/components/SubBrandLogoWordMark.txt +125 -0
- package/mcp-docs/mobile/components/Switch.txt +97 -0
- package/mcp-docs/mobile/components/TabIndicator.txt +48 -0
- package/mcp-docs/mobile/components/TabLabel.txt +153 -0
- package/mcp-docs/mobile/components/TabNavigation.txt +146 -0
- package/mcp-docs/mobile/components/TabbedChips.txt +142 -0
- package/mcp-docs/mobile/components/Tabs.txt +190 -0
- package/mcp-docs/mobile/components/Tag.txt +300 -0
- package/mcp-docs/mobile/components/Text.txt +211 -0
- package/mcp-docs/mobile/components/TextInput.txt +717 -0
- package/mcp-docs/mobile/components/ThemeProvider.txt +132 -0
- package/mcp-docs/mobile/components/Toast.txt +196 -0
- package/mcp-docs/mobile/components/Tooltip.txt +59 -0
- package/mcp-docs/mobile/components/TopNavBar.txt +161 -0
- package/mcp-docs/mobile/components/Tour.txt +158 -0
- package/mcp-docs/mobile/components/Tray.txt +252 -0
- package/mcp-docs/mobile/components/UpsellCard.txt +321 -0
- package/mcp-docs/mobile/components/VStack.txt +222 -0
- package/mcp-docs/mobile/components/XAxis.txt +621 -0
- package/mcp-docs/mobile/components/YAxis.txt +567 -0
- package/mcp-docs/mobile/getting-started/ai-overview.txt +108 -0
- package/mcp-docs/mobile/getting-started/installation.txt +57 -0
- package/mcp-docs/mobile/getting-started/introduction.txt +102 -0
- package/mcp-docs/mobile/getting-started/playground.txt +28 -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 +72 -0
- 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 +280 -0
- 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 +321 -0
- package/mcp-docs/mobile/routes.txt +139 -0
- package/mcp-docs/web/components/Accordion.txt +189 -0
- package/mcp-docs/web/components/AccordionItem.txt +31 -0
- package/mcp-docs/web/components/Alert.txt +164 -0
- package/mcp-docs/web/components/AreaChart.txt +510 -0
- package/mcp-docs/web/components/Avatar.txt +211 -0
- package/mcp-docs/web/components/AvatarButton.txt +240 -0
- package/mcp-docs/web/components/Banner.txt +226 -0
- package/mcp-docs/web/components/BarChart.txt +1267 -0
- package/mcp-docs/web/components/Box.txt +175 -0
- package/mcp-docs/web/components/Button.txt +212 -0
- package/mcp-docs/web/components/ButtonGroup.txt +79 -0
- package/mcp-docs/web/components/Calendar.txt +181 -0
- package/mcp-docs/web/components/Carousel.txt +1575 -0
- package/mcp-docs/web/components/CartesianChart.txt +1044 -0
- package/mcp-docs/web/components/CellMedia.txt +56 -0
- package/mcp-docs/web/components/Checkbox.txt +188 -0
- package/mcp-docs/web/components/CheckboxCell.txt +202 -0
- package/mcp-docs/web/components/CheckboxGroup.txt +219 -0
- package/mcp-docs/web/components/Chip.txt +196 -0
- package/mcp-docs/web/components/Coachmark.txt +188 -0
- package/mcp-docs/web/components/Collapsible.txt +119 -0
- package/mcp-docs/web/components/ContainedAssetCard.txt +232 -0
- package/mcp-docs/web/components/ContentCard.txt +367 -0
- package/mcp-docs/web/components/ContentCardBody.txt +137 -0
- package/mcp-docs/web/components/ContentCardFooter.txt +129 -0
- package/mcp-docs/web/components/ContentCardHeader.txt +147 -0
- package/mcp-docs/web/components/ContentCell.txt +219 -0
- package/mcp-docs/web/components/ControlGroup.txt +436 -0
- package/mcp-docs/web/components/DatePicker.txt +505 -0
- package/mcp-docs/web/components/Divider.txt +143 -0
- package/mcp-docs/web/components/DotCount.txt +149 -0
- package/mcp-docs/web/components/DotStatusColor.txt +58 -0
- package/mcp-docs/web/components/DotSymbol.txt +137 -0
- package/mcp-docs/web/components/Dropdown.txt +119 -0
- package/mcp-docs/web/components/Fallback.txt +163 -0
- package/mcp-docs/web/components/FloatingAssetCard.txt +250 -0
- package/mcp-docs/web/components/FullscreenAlert.txt +69 -0
- package/mcp-docs/web/components/FullscreenModal.txt +145 -0
- package/mcp-docs/web/components/FullscreenModalLayout.txt +187 -0
- package/mcp-docs/web/components/Grid.txt +236 -0
- package/mcp-docs/web/components/GridColumn.txt +209 -0
- package/mcp-docs/web/components/HStack.txt +236 -0
- package/mcp-docs/web/components/HeroSquare.txt +48 -0
- package/mcp-docs/web/components/Icon.txt +145 -0
- package/mcp-docs/web/components/IconButton.txt +390 -0
- package/mcp-docs/web/components/InputChip.txt +187 -0
- package/mcp-docs/web/components/Interactable.txt +193 -0
- package/mcp-docs/web/components/LineChart.txt +1576 -0
- package/mcp-docs/web/components/Link.txt +243 -0
- package/mcp-docs/web/components/ListCell.txt +418 -0
- package/mcp-docs/web/components/LogoMark.txt +84 -0
- package/mcp-docs/web/components/LogoWordMark.txt +93 -0
- package/mcp-docs/web/components/Lottie.txt +157 -0
- package/mcp-docs/web/components/LottieStatusAnimation.txt +57 -0
- package/mcp-docs/web/components/MediaQueryProvider.txt +108 -0
- package/mcp-docs/web/components/Modal.txt +196 -0
- package/mcp-docs/web/components/ModalBody.txt +117 -0
- package/mcp-docs/web/components/ModalFooter.txt +119 -0
- package/mcp-docs/web/components/ModalHeader.txt +123 -0
- package/mcp-docs/web/components/MultiContentModule.txt +381 -0
- package/mcp-docs/web/components/NavigationBar.txt +102 -0
- package/mcp-docs/web/components/NavigationTitle.txt +25 -0
- package/mcp-docs/web/components/NavigationTitleSelect.txt +45 -0
- package/mcp-docs/web/components/NudgeCard.txt +181 -0
- package/mcp-docs/web/components/Overlay.txt +171 -0
- package/mcp-docs/web/components/PageFooter.txt +184 -0
- package/mcp-docs/web/components/PageHeader.txt +243 -0
- package/mcp-docs/web/components/Pagination.txt +499 -0
- package/mcp-docs/web/components/PeriodSelector.txt +703 -0
- package/mcp-docs/web/components/Pictogram.txt +48 -0
- package/mcp-docs/web/components/Point.txt +460 -0
- package/mcp-docs/web/components/PortalProvider.txt +76 -0
- package/mcp-docs/web/components/Pressable.txt +193 -0
- package/mcp-docs/web/components/ProgressBar.txt +163 -0
- package/mcp-docs/web/components/ProgressBarWithFixedLabels.txt +212 -0
- package/mcp-docs/web/components/ProgressBarWithFloatLabel.txt +181 -0
- package/mcp-docs/web/components/ProgressCircle.txt +443 -0
- package/mcp-docs/web/components/Radio.txt +219 -0
- package/mcp-docs/web/components/RadioCell.txt +215 -0
- package/mcp-docs/web/components/RadioGroup.txt +288 -0
- package/mcp-docs/web/components/ReferenceLine.txt +451 -0
- package/mcp-docs/web/components/RemoteImage.txt +165 -0
- package/mcp-docs/web/components/RemoteImageGroup.txt +86 -0
- package/mcp-docs/web/components/RollingNumber.txt +1021 -0
- package/mcp-docs/web/components/Scrubber.txt +231 -0
- package/mcp-docs/web/components/SearchInput.txt +117 -0
- package/mcp-docs/web/components/SectionHeader.txt +217 -0
- package/mcp-docs/web/components/SegmentedTabs.txt +324 -0
- package/mcp-docs/web/components/Select.txt +224 -0
- package/mcp-docs/web/components/SelectChip.txt +314 -0
- package/mcp-docs/web/components/SelectOption.txt +165 -0
- package/mcp-docs/web/components/Sidebar.txt +349 -0
- package/mcp-docs/web/components/SidebarItem.txt +131 -0
- package/mcp-docs/web/components/SidebarMoreMenu.txt +30 -0
- package/mcp-docs/web/components/Spacer.txt +173 -0
- package/mcp-docs/web/components/Sparkline.txt +122 -0
- package/mcp-docs/web/components/SparklineGradient.txt +106 -0
- package/mcp-docs/web/components/SparklineInteractive.txt +153 -0
- package/mcp-docs/web/components/SparklineInteractiveHeader.txt +76 -0
- package/mcp-docs/web/components/Spinner.txt +128 -0
- package/mcp-docs/web/components/SpotIcon.txt +48 -0
- package/mcp-docs/web/components/SpotRectangle.txt +48 -0
- package/mcp-docs/web/components/SpotSquare.txt +48 -0
- package/mcp-docs/web/components/Stepper.txt +682 -0
- package/mcp-docs/web/components/SubBrandLogoMark.txt +125 -0
- package/mcp-docs/web/components/SubBrandLogoWordMark.txt +125 -0
- package/mcp-docs/web/components/Switch.txt +85 -0
- package/mcp-docs/web/components/TabIndicator.txt +48 -0
- package/mcp-docs/web/components/TabLabel.txt +158 -0
- package/mcp-docs/web/components/TabNavigation.txt +159 -0
- package/mcp-docs/web/components/TabbedChips.txt +155 -0
- package/mcp-docs/web/components/Table.txt +367 -0
- package/mcp-docs/web/components/TableBody.txt +83 -0
- package/mcp-docs/web/components/TableCaption.txt +102 -0
- package/mcp-docs/web/components/TableCell.txt +165 -0
- package/mcp-docs/web/components/TableCellFallback.txt +97 -0
- package/mcp-docs/web/components/TableFooter.txt +83 -0
- package/mcp-docs/web/components/TableHeader.txt +100 -0
- package/mcp-docs/web/components/TableRow.txt +140 -0
- package/mcp-docs/web/components/Tabs.txt +212 -0
- package/mcp-docs/web/components/Tag.txt +304 -0
- package/mcp-docs/web/components/Text.txt +232 -0
- package/mcp-docs/web/components/TextInput.txt +652 -0
- package/mcp-docs/web/components/ThemeProvider.txt +199 -0
- package/mcp-docs/web/components/TileButton.txt +158 -0
- package/mcp-docs/web/components/Toast.txt +203 -0
- package/mcp-docs/web/components/Tooltip.txt +89 -0
- package/mcp-docs/web/components/Tour.txt +179 -0
- package/mcp-docs/web/components/Tray.txt +288 -0
- package/mcp-docs/web/components/UpsellCard.txt +319 -0
- package/mcp-docs/web/components/VStack.txt +224 -0
- package/mcp-docs/web/components/XAxis.txt +619 -0
- package/mcp-docs/web/components/YAxis.txt +548 -0
- package/mcp-docs/web/getting-started/ai-overview.txt +108 -0
- package/mcp-docs/web/getting-started/installation.txt +103 -0
- package/mcp-docs/web/getting-started/introduction.txt +102 -0
- package/mcp-docs/web/getting-started/playground.txt +28 -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 +61 -0
- package/mcp-docs/web/hooks/useDimensions.txt +114 -0
- package/mcp-docs/web/hooks/useEventHandler.txt +120 -0
- package/mcp-docs/web/hooks/useHasMounted.txt +75 -0
- package/mcp-docs/web/hooks/useIsoEffect.txt +58 -0
- package/mcp-docs/web/hooks/useMediaQuery.txt +114 -0
- package/mcp-docs/web/hooks/useMergeRefs.txt +116 -0
- package/mcp-docs/web/hooks/useOverlayContentContext.txt +279 -0
- 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 +82 -0
- package/mcp-docs/web/hooks/useTheme.txt +364 -0
- package/mcp-docs/web/routes.txt +163 -0
- 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
|
+
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# useBreakpoints
|
|
2
|
+
|
|
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
|
+
|
|
5
|
+
## Import
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import { useBreakpoints } from '@coinbase/cds-web/hooks/useBreakpoints'
|
|
9
|
+
```
|
|
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
|
+
|
|
40
|
+
## Examples
|
|
41
|
+
|
|
42
|
+
import { JSONCodeBlock } from '@site/src/components/page/JSONCodeBlock';
|
|
43
|
+
import { useBreakpoints } from '@coinbase/cds-web/hooks/useBreakpoints';
|
|
44
|
+
|
|
45
|
+
### Basic usage
|
|
46
|
+
|
|
47
|
+
Use the `useBreakpoints` hook to conditionally render components or component trees based on the current window breakpoint.
|
|
48
|
+
|
|
49
|
+
[See how breakpoints are defined in the `media` object →](https://github.com/coinbase/cds/blob/master/packages/web/src/styles/media.ts)
|
|
50
|
+
|
|
51
|
+
:::warning
|
|
52
|
+
Do not use `useBreakpoints` for responsive styles. Use CSS media queries or [the `StyleProps` API](/getting-started/styling#responsive-styles) for responsive styles.
|
|
53
|
+
:::
|
|
54
|
+
|
|
55
|
+
```tsx live
|
|
56
|
+
() => {
|
|
57
|
+
const breakpoints = useBreakpoints();
|
|
58
|
+
return <JSONCodeBlock json={breakpoints} />;
|
|
59
|
+
};
|
|
60
|
+
```
|
|
61
|
+
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# useDimensions
|
|
2
|
+
|
|
3
|
+
Measures an element's dimensions using ResizeObserver.
|
|
4
|
+
|
|
5
|
+
## Import
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import { useDimensions } from '@coinbase/cds-web/hooks/useDimensions'
|
|
9
|
+
```
|
|
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
|
+
|
|
71
|
+
## Examples
|
|
72
|
+
|
|
73
|
+
### Basic usage
|
|
74
|
+
|
|
75
|
+
```tsx live
|
|
76
|
+
function Example() {
|
|
77
|
+
const ref = useRef(null);
|
|
78
|
+
const { width, height } = useDimensions({ ref });
|
|
79
|
+
|
|
80
|
+
return (
|
|
81
|
+
<Box ref={ref} padding={3} background="bgAlternate" borderRadius={300} width="100%">
|
|
82
|
+
<TextHeadline>
|
|
83
|
+
This box is {width}px wide and {height}px tall
|
|
84
|
+
</TextHeadline>
|
|
85
|
+
</Box>
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### With Breakpoints
|
|
91
|
+
|
|
92
|
+
```tsx live
|
|
93
|
+
function Example() {
|
|
94
|
+
const ref = useRef(null);
|
|
95
|
+
const { width, currentBreakpoint } = useDimensions({
|
|
96
|
+
ref,
|
|
97
|
+
breakpoints: {
|
|
98
|
+
small: 300,
|
|
99
|
+
medium: 400,
|
|
100
|
+
large: 500,
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
return (
|
|
105
|
+
<Box ref={ref} padding={3} background="bgAlternate" borderRadius={300} width="100%">
|
|
106
|
+
<VStack gap={2}>
|
|
107
|
+
<TextHeadline>Width: {width}px</TextHeadline>
|
|
108
|
+
<TextHeadline>Current breakpoint: {currentBreakpoint || 'none'}</TextHeadline>
|
|
109
|
+
</VStack>
|
|
110
|
+
</Box>
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|