@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,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
|
+
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# useHasMounted
|
|
2
|
+
|
|
3
|
+
Returns a boolean indicating if the component has mounted.
|
|
4
|
+
|
|
5
|
+
## Import
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import { useHasMounted } from '@coinbase/cds-web/hooks/useHasMounted'
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## API
|
|
12
|
+
|
|
13
|
+
### Parameters
|
|
14
|
+
|
|
15
|
+
The `useHasMounted` hook does not accept any parameters.
|
|
16
|
+
|
|
17
|
+
### Returns
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
type Return = boolean;
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Returns a boolean:
|
|
24
|
+
|
|
25
|
+
- `true`: Component has mounted and is running in a browser environment
|
|
26
|
+
- `false`: Component hasn't mounted yet or is running in a non-browser environment (e.g., server-side rendering)
|
|
27
|
+
|
|
28
|
+
:::tip
|
|
29
|
+
This hook is useful for preventing hydration mismatches and ensuring code only runs in the browser environment.
|
|
30
|
+
:::
|
|
31
|
+
|
|
32
|
+
## Examples
|
|
33
|
+
|
|
34
|
+
`useHasMounted` should be used when conditionally rendering components or styles in SSR or SSG environments.
|
|
35
|
+
|
|
36
|
+
We recommend using `useHasMounted` whenever you use [useBreakpoints](/hooks/useBreakpoints) or any other hooks that rely on the window object to conditionally render content. This combination can be used to prevent cumulative layout shifts (CLS). This is called two pass rendering and ensures that the component has been mounted and the window object is present before painting the DOM.
|
|
37
|
+
|
|
38
|
+
```jsx
|
|
39
|
+
const { isPhone } = useBreakpoints();
|
|
40
|
+
const hasMounted = useHasMounted();
|
|
41
|
+
|
|
42
|
+
// in component render
|
|
43
|
+
{
|
|
44
|
+
hasMounted && isPhone && <TextHeadline as="h3">Welcome {username}!</TextHeadline>;
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Basic usage
|
|
49
|
+
|
|
50
|
+
```tsx live
|
|
51
|
+
function Example() {
|
|
52
|
+
const hasMounted = useHasMounted();
|
|
53
|
+
|
|
54
|
+
return <TextHeadline>Component has {hasMounted ? 'mounted' : 'not mounted'}</TextHeadline>;
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Preventing Hydration Mismatch
|
|
59
|
+
|
|
60
|
+
```tsx live
|
|
61
|
+
function Example() {
|
|
62
|
+
const hasMounted = useHasMounted();
|
|
63
|
+
const [currentTime, setCurrentTime] = useState('');
|
|
64
|
+
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
// Only run client-side code after mounting
|
|
67
|
+
if (hasMounted) {
|
|
68
|
+
setCurrentTime(new Date().toLocaleTimeString());
|
|
69
|
+
}
|
|
70
|
+
}, [hasMounted]);
|
|
71
|
+
|
|
72
|
+
return <TextHeadline>{hasMounted ? `Current time: ${currentTime}` : 'Loading...'}</TextHeadline>;
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# useIsoEffect
|
|
2
|
+
|
|
3
|
+
A safe way of calling useLayoutEffect only on the client. Does nothing on the server.
|
|
4
|
+
|
|
5
|
+
## Import
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import { useIsoEffect } from '@coinbase/cds-web/hooks/useIsoEffect'
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## API
|
|
12
|
+
|
|
13
|
+
### Parameters
|
|
14
|
+
|
|
15
|
+
The `useIsoEffect` hook accepts the same parameters as React's `useEffect` and `useLayoutEffect`:
|
|
16
|
+
|
|
17
|
+
- `effect: () => (void | (() => void))` - The effect function to run
|
|
18
|
+
- `deps?: any[]` - Optional array of dependencies
|
|
19
|
+
|
|
20
|
+
### Returns
|
|
21
|
+
|
|
22
|
+
Returns nothing (void).
|
|
23
|
+
|
|
24
|
+
:::tip
|
|
25
|
+
This hook automatically uses `useLayoutEffect` when running in the browser and falls back to `useEffect` during server-side rendering. This prevents warnings and ensures proper timing of effects across different environments.
|
|
26
|
+
:::
|
|
27
|
+
|
|
28
|
+
## Examples
|
|
29
|
+
|
|
30
|
+
### Usage
|
|
31
|
+
|
|
32
|
+
```tsx live
|
|
33
|
+
function Example() {
|
|
34
|
+
const [position, setPosition] = useState({ x: 0, y: 0 });
|
|
35
|
+
|
|
36
|
+
useIsoEffect(() => {
|
|
37
|
+
const updatePosition = (e: MouseEvent) => {
|
|
38
|
+
setPosition({ x: e.clientX, y: e.clientY });
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
window.addEventListener('mousemove', updatePosition);
|
|
42
|
+
|
|
43
|
+
// Cleanup function to remove event listener
|
|
44
|
+
return () => {
|
|
45
|
+
window.removeEventListener('mousemove', updatePosition);
|
|
46
|
+
};
|
|
47
|
+
}, []); // Empty deps array means this effect runs once on mount
|
|
48
|
+
|
|
49
|
+
return (
|
|
50
|
+
<VStack gap={2}>
|
|
51
|
+
<TextHeadline>Mouse Position</TextHeadline>
|
|
52
|
+
<Text>X: {position.x}</Text>
|
|
53
|
+
<Text>Y: {position.y}</Text>
|
|
54
|
+
</VStack>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# useMediaQuery
|
|
2
|
+
|
|
3
|
+
Subscribes to window.matchMedia changes with SSR support.
|
|
4
|
+
|
|
5
|
+
## Import
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import { useMediaQuery } from '@coinbase/cds-web/hooks/useMediaQuery'
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## API
|
|
12
|
+
|
|
13
|
+
### Parameters
|
|
14
|
+
|
|
15
|
+
The `useMediaQuery` hook accepts a single parameter:
|
|
16
|
+
|
|
17
|
+
- `query: string` - A CSS media query string to subscribe to. Can include:
|
|
18
|
+
- **Viewport dimensions**: `(min-width: 768px)`, `(max-width: 767px)`, `(width: 1024px)`
|
|
19
|
+
- **User preferences**: `(prefers-color-scheme: dark)`, `(prefers-reduced-motion: reduce)`
|
|
20
|
+
- **Device features**: `(orientation: portrait)`, `(pointer: coarse)`, `(hover: hover)`
|
|
21
|
+
- **Complex queries**: Combined with `and`, `not`, or `,` operators
|
|
22
|
+
|
|
23
|
+
### Returns
|
|
24
|
+
|
|
25
|
+
Returns a `boolean` that indicates whether the media query matches:
|
|
26
|
+
|
|
27
|
+
- `true` - The media query conditions are currently met
|
|
28
|
+
- `false` - The media query conditions are not met
|
|
29
|
+
- Automatically updates when the media query state changes
|
|
30
|
+
- Uses MediaQueryProvider's default values during server-side rendering
|
|
31
|
+
|
|
32
|
+
## Examples
|
|
33
|
+
|
|
34
|
+
### Basic usage
|
|
35
|
+
|
|
36
|
+
Use the `useMediaQuery` hook to call `window.matchMedia` with SSR support. It must be used within a MediaQueryProvider component.
|
|
37
|
+
|
|
38
|
+
This hook is ideal for conditional rendering based on viewport size, user preferences, or other media features.
|
|
39
|
+
|
|
40
|
+
It subscribes to a single state shared by all media queries to ensure Suspense works correctly.
|
|
41
|
+
|
|
42
|
+
[See the MediaQueryProvider docs here →](/components/other/MediaQueryProvider)
|
|
43
|
+
|
|
44
|
+
:::warning
|
|
45
|
+
Do not use `useMediaQuery` for responsive styles. Use CSS media queries or [the `StyleProps` API](/getting-started/styling#responsive-styles) for responsive styles.
|
|
46
|
+
:::
|
|
47
|
+
|
|
48
|
+
```tsx live
|
|
49
|
+
() => {
|
|
50
|
+
const Page = () => {
|
|
51
|
+
const isMobile = useMediaQuery('(max-width: 767px)');
|
|
52
|
+
const prefersDarkMode = useMediaQuery('(prefers-color-scheme: dark)');
|
|
53
|
+
return <JSONCodeBlock json={{ isMobile, prefersDarkMode }} />;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const App = () => (
|
|
57
|
+
<MediaQueryProvider>
|
|
58
|
+
<Page />
|
|
59
|
+
</MediaQueryProvider>
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
return <App />;
|
|
63
|
+
};
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### SSR support
|
|
67
|
+
|
|
68
|
+
The `useMediaQuery` hook integrates with the MediaQueryProvider `defaultValues` to provide consistent behavior between server and client rendering.
|
|
69
|
+
|
|
70
|
+
On the client the native `window.matchMedia` API is used. On the server the hook solves the `window.matchMedia` queries by comparing to the MediaQueryProvider `defaultValues`.
|
|
71
|
+
|
|
72
|
+
The comparison against `defaultValues` during SSR is limited: it cannot solve highly complex media queries. If a complex query cannot be solved during SSR the hook will simply return `false`, and the query can still be solved by `window.matchMedia` on the client.
|
|
73
|
+
|
|
74
|
+
:::tip
|
|
75
|
+
You can populate the MediaQueryProvider `defaultValues` with user preferences, cookies, etc. to ensure the correct styles are applied on the server.
|
|
76
|
+
:::
|
|
77
|
+
|
|
78
|
+
#### Simple queries that can be solved during SSR
|
|
79
|
+
|
|
80
|
+
- Simple media queries
|
|
81
|
+
- `width`, `min-width`, `max-width`, `height`, `min-height`, and `max-height` with pixel or em units
|
|
82
|
+
- `prefers-contrast` and `prefers-color-scheme`
|
|
83
|
+
- Logical `and` operator
|
|
84
|
+
|
|
85
|
+
#### Complex queries that cannot be solved during SSR
|
|
86
|
+
|
|
87
|
+
- Multiple comma-delimited values
|
|
88
|
+
- Logical `not` and `or` operators
|
|
89
|
+
- Mathematical `<=` and `>=` operators
|
|
90
|
+
- Complex or nested queries
|
|
91
|
+
- Other media types or features
|
|
92
|
+
|
|
93
|
+
[See the MediaQueryProvider SSR docs here →](/components/other/MediaQueryProvider#ssr-support)
|
|
94
|
+
|
|
95
|
+
### Complex queries on the client
|
|
96
|
+
|
|
97
|
+
Complex queries cannot be solved during SSR. They are solved on the client by calling `window.matchMedia`.
|
|
98
|
+
|
|
99
|
+
```tsx live
|
|
100
|
+
() => {
|
|
101
|
+
const isPortrait = useMediaQuery('(orientation: portrait)');
|
|
102
|
+
const isHighDPI = useMediaQuery('(min-resolution: 2dppx)');
|
|
103
|
+
const isTouch = useMediaQuery('(pointer: coarse)');
|
|
104
|
+
const isMediumHeight = useMediaQuery('(min-height: 600px) and (max-height: 900px)');
|
|
105
|
+
|
|
106
|
+
const complexQuery = useMediaQuery(
|
|
107
|
+
`((width >= 1200px) and (orientation: landscape)),
|
|
108
|
+
(width < 560px) or ((width > 768px) and (width < 900px))`,
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
return <JSONCodeBlock json={{ isPortrait, isHighDPI, isTouch, isMediumHeight, complexQuery }} />;
|
|
112
|
+
};
|
|
113
|
+
```
|
|
114
|
+
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# useMergeRefs
|
|
2
|
+
|
|
3
|
+
Combines multiple refs into a single ref callback, allowing a component to work with multiple ref instances simultaneously. Useful when you need to combine refs from different sources, such as forwarded refs and internal component state.
|
|
4
|
+
|
|
5
|
+
## Import
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import { useMergeRefs } from '@coinbase/cds-common/hooks/useMergeRefs'
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## API
|
|
12
|
+
|
|
13
|
+
### Parameters
|
|
14
|
+
|
|
15
|
+
The `useMergeRefs` hook accepts a spread of refs as its parameter:
|
|
16
|
+
|
|
17
|
+
- `...refs: (React.MutableRefObject<T> | React.LegacyRef<T> | undefined | null)[]` - An array of refs to merge. Can include:
|
|
18
|
+
- `MutableRefObject` - Object-based refs created with `useRef`
|
|
19
|
+
- `LegacyRef` - Function-based refs or string refs (legacy)
|
|
20
|
+
- `undefined` or `null` - Optional refs that might not be provided
|
|
21
|
+
|
|
22
|
+
### Returns
|
|
23
|
+
|
|
24
|
+
Returns a `React.RefCallback<T>` function that can be used as a ref:
|
|
25
|
+
|
|
26
|
+
- The returned callback handles updating all provided refs when the referenced element changes
|
|
27
|
+
- Properly handles both object-based and function-based refs
|
|
28
|
+
- Safely handles undefined or null refs by ignoring them
|
|
29
|
+
|
|
30
|
+
:::tip
|
|
31
|
+
This hook is particularly useful when working with components that need to combine multiple refs, such as when using both forwarded refs and internal state refs, or when composing components that each require their own ref.
|
|
32
|
+
:::
|
|
33
|
+
|
|
34
|
+
## Examples
|
|
35
|
+
|
|
36
|
+
### Basic usage
|
|
37
|
+
|
|
38
|
+
```tsx live
|
|
39
|
+
function Example() {
|
|
40
|
+
// Create an internal ref for component logic
|
|
41
|
+
const internalRef = useRef<HTMLDivElement>(null);
|
|
42
|
+
|
|
43
|
+
// Simulating a forwarded ref from parent
|
|
44
|
+
const Component = forwardRef((props, forwardedRef) => {
|
|
45
|
+
// Merge the internal ref with the forwarded ref
|
|
46
|
+
const refs = useMergeRefs(forwardedRef, internalRef);
|
|
47
|
+
|
|
48
|
+
return (
|
|
49
|
+
<Box ref={refs} padding={3} background="bgAlternate" borderRadius={300}>
|
|
50
|
+
<TextBody>This box uses a merged ref</TextBody>
|
|
51
|
+
</Box>
|
|
52
|
+
);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
return <Component />;
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### With Multiple Refs
|
|
60
|
+
|
|
61
|
+
```tsx live
|
|
62
|
+
function Example() {
|
|
63
|
+
const firstRef = useRef<HTMLDivElement>(null);
|
|
64
|
+
const secondRef = useRef<HTMLDivElement>(null);
|
|
65
|
+
const thirdRef = useRef<HTMLDivElement>(null);
|
|
66
|
+
|
|
67
|
+
// Function to check if all refs are properly set
|
|
68
|
+
const checkRefs = () => {
|
|
69
|
+
const allRefsSet = firstRef.current && secondRef.current && thirdRef.current;
|
|
70
|
+
alert(`All refs are ${allRefsSet ? 'set' : 'not set'}`);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// Merge all three refs
|
|
74
|
+
const mergedRefs = useMergeRefs(firstRef, secondRef, thirdRef);
|
|
75
|
+
|
|
76
|
+
return (
|
|
77
|
+
<VStack gap={2}>
|
|
78
|
+
<Box ref={mergedRefs} padding={3} background="bgAlternate" borderRadius={300}>
|
|
79
|
+
<TextBody>This element is referenced by three refs</TextBody>
|
|
80
|
+
</Box>
|
|
81
|
+
<Button onClick={checkRefs}>Check Refs</Button>
|
|
82
|
+
</VStack>
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### With Function Ref
|
|
88
|
+
|
|
89
|
+
```tsx live
|
|
90
|
+
function Example() {
|
|
91
|
+
const [elementWidth, setElementWidth] = useState<number>(0);
|
|
92
|
+
|
|
93
|
+
// Create a function ref that measures the element
|
|
94
|
+
const functionRef = (element: HTMLDivElement | null) => {
|
|
95
|
+
if (element) {
|
|
96
|
+
setElementWidth(element.offsetWidth);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// Create an object ref for other purposes
|
|
101
|
+
const objectRef = useRef<HTMLDivElement>(null);
|
|
102
|
+
|
|
103
|
+
// Merge both types of refs
|
|
104
|
+
const refs = useMergeRefs(functionRef, objectRef);
|
|
105
|
+
|
|
106
|
+
return (
|
|
107
|
+
<VStack gap={2}>
|
|
108
|
+
<Box ref={refs} padding={3} background="bgAlternate" borderRadius={300} width="100%">
|
|
109
|
+
<TextBody>This box uses both function and object refs</TextBody>
|
|
110
|
+
</Box>
|
|
111
|
+
<TextBody>Box width: {elementWidth}px</TextBody>
|
|
112
|
+
</VStack>
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|