@ds-mo/ui 0.2.3 → 0.2.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/dist/r/toast.json CHANGED
@@ -70,7 +70,7 @@
70
70
  },
71
71
  {
72
72
  "path": "src/components/Toast/Toast.tsx",
73
- "content": "import React, { forwardRef, useEffect, useState, useCallback, useRef, useSyncExternalStore } from 'react';\nimport ReactDOM from 'react-dom';\nimport { cn } from '@/utils/cn';\nimport { Text } from '@/components/Text';\nimport { Surface } from '@/components/Surface';\nimport type { SurfaceIntent } from '@/components/Surface';\nimport styles from './Toast.module.css';\n\n/* ─── Toast Types ───────────────────────────────────────────────────────────── */\n\nexport type ToastIntent = 'neutral' | 'brand' | 'positive' | 'negative' | 'warning' | 'caution';\nexport type ToastPosition = 'top-center' | 'top-right' | 'bottom-center' | 'bottom-right';\n\nexport interface ToastData {\n id: string;\n message: string;\n intent?: ToastIntent;\n duration?: number;\n onDismiss?: () => void;\n}\n\nexport interface ToastOptions {\n message: string;\n intent?: ToastIntent;\n /** Auto-dismiss duration in ms. 0 = no auto-dismiss. Default 4000. */\n duration?: number;\n onDismiss?: () => void;\n}\n\n/* ─── Toast Store (external, framework-agnostic) ────────────────────────────── */\n\nlet toasts: ToastData[] = [];\nconst listeners = new Set<() => void>();\n\nfunction emit() {\n listeners.forEach(fn => fn());\n}\n\nfunction getSnapshot(): ToastData[] {\n return toasts;\n}\n\nfunction subscribe(listener: () => void) {\n listeners.add(listener);\n return () => { listeners.delete(listener); };\n}\n\nlet idCounter = 0;\n\nexport const toast = {\n show(options: ToastOptions): string {\n const id = `toast-${++idCounter}`;\n toasts = [...toasts, { id, ...options }];\n emit();\n return id;\n },\n info(message: string, options?: Omit<ToastOptions, 'message'>): string {\n return toast.show({ message, intent: 'neutral', ...options });\n },\n success(message: string, options?: Omit<ToastOptions, 'message'>): string {\n return toast.show({ message, intent: 'positive', ...options });\n },\n error(message: string, options?: Omit<ToastOptions, 'message'>): string {\n return toast.show({ message, intent: 'negative', ...options });\n },\n warning(message: string, options?: Omit<ToastOptions, 'message'>): string {\n return toast.show({ message, intent: 'warning', ...options });\n },\n dismiss(id: string) {\n toasts = toasts.filter(t => t.id !== id);\n emit();\n },\n dismissAll() {\n toasts = [];\n emit();\n },\n};\n\n/* ─── useToasts hook ────────────────────────────────────────────────────────── */\n\nexport function useToasts(): ToastData[] {\n return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n}\n\n/* ─── Single Toast ──────────────────────────────────────────────────────────── */\n\nconst INTENT_MAP: Record<ToastIntent, SurfaceIntent> = {\n neutral: 'neutral',\n brand: 'brand',\n positive: 'positive',\n negative: 'negative',\n warning: 'warning',\n caution: 'caution',\n};\n\ninterface ToastItemProps {\n data: ToastData;\n}\n\nconst ToastItem: React.FC<ToastItemProps> = ({ data }) => {\n const [exiting, setExiting] = useState(false);\n const timerRef = useRef<ReturnType<typeof setTimeout>>();\n\n const dismiss = useCallback(() => {\n setExiting(true);\n setTimeout(() => {\n toast.dismiss(data.id);\n data.onDismiss?.();\n }, 200);\n }, [data]);\n\n useEffect(() => {\n const duration = data.duration ?? 4000;\n if (duration > 0) {\n timerRef.current = setTimeout(dismiss, duration);\n }\n return () => { if (timerRef.current) clearTimeout(timerRef.current); };\n }, [data.duration, dismiss]);\n\n return (\n <Surface\n intent={INTENT_MAP[data.intent ?? 'neutral']}\n contrast=\"bold\"\n elevation=\"floating\"\n radius=\"lg\"\n className={cn(styles.toast, exiting && styles.toastExit)}\n role=\"status\"\n aria-live=\"polite\"\n >\n <Text variant=\"text-body-medium\" as=\"span\" color=\"inherit\">\n {data.message}\n </Text>\n <button\n type=\"button\"\n className={styles.dismiss}\n onClick={dismiss}\n aria-label=\"Dismiss\"\n >\n &times;\n </button>\n </Surface>\n );\n};\n\n/* ─── ToastContainer ────────────────────────────────────────────────────────── */\n\nexport interface ToastContainerProps {\n /** Where to render toasts on screen. */\n position?: ToastPosition;\n}\n\nexport const ToastContainer = forwardRef<HTMLDivElement, ToastContainerProps>(\n ({ position = 'top-center' }, ref) => {\n const items = useToasts();\n\n if (typeof document === 'undefined') return null;\n\n return ReactDOM.createPortal(\n <div\n ref={ref}\n className={cn(styles.container, styles[positionClass(position)])}\n aria-label=\"Notifications\"\n >\n {items.map(item => (\n <ToastItem key={item.id} data={item} />\n ))}\n </div>,\n document.body\n );\n }\n);\n\nToastContainer.displayName = 'ToastContainer';\n\nfunction positionClass(position: ToastPosition): string {\n switch (position) {\n case 'top-center': return 'topCenter';\n case 'top-right': return 'topRight';\n case 'bottom-center': return 'bottomCenter';\n case 'bottom-right': return 'bottomRight';\n }\n}\n",
73
+ "content": "import React, { forwardRef, useEffect, useState, useCallback, useRef, useSyncExternalStore } from 'react';\nimport ReactDOM from 'react-dom';\nimport { cn } from '@/utils/cn';\nimport { Text } from '@/components/Text';\nimport { Surface } from '@/components/Surface';\nimport type { SurfaceIntent } from '@/components/Surface';\nimport styles from './Toast.module.css';\n\n/* ─── Toast Types ───────────────────────────────────────────────────────────── */\n\nexport type ToastIntent = 'neutral' | 'brand' | 'positive' | 'negative' | 'warning' | 'caution';\nexport type ToastPosition = 'top-center' | 'top-right' | 'bottom-center' | 'bottom-right';\n\nexport interface ToastData {\n id: string;\n message: string;\n intent?: ToastIntent;\n duration?: number;\n onDismiss?: () => void;\n}\n\nexport interface ToastOptions {\n message: string;\n intent?: ToastIntent;\n /** Auto-dismiss duration in ms. 0 = no auto-dismiss. Default 4000. */\n duration?: number;\n onDismiss?: () => void;\n}\n\n/* ─── Toast Store (external, framework-agnostic) ────────────────────────────── */\n\nlet toasts: ToastData[] = [];\nconst listeners = new Set<() => void>();\n\nfunction emit() {\n listeners.forEach(fn => fn());\n}\n\nfunction getSnapshot(): ToastData[] {\n return toasts;\n}\n\nfunction subscribe(listener: () => void) {\n listeners.add(listener);\n return () => { listeners.delete(listener); };\n}\n\nlet idCounter = 0;\n\nexport const toast = {\n show(options: ToastOptions): string {\n const id = `toast-${++idCounter}`;\n toasts = [...toasts, { id, ...options }];\n emit();\n return id;\n },\n info(message: string, options?: Omit<ToastOptions, 'message'>): string {\n return toast.show({ message, intent: 'neutral', ...options });\n },\n success(message: string, options?: Omit<ToastOptions, 'message'>): string {\n return toast.show({ message, intent: 'positive', ...options });\n },\n error(message: string, options?: Omit<ToastOptions, 'message'>): string {\n return toast.show({ message, intent: 'negative', ...options });\n },\n warning(message: string, options?: Omit<ToastOptions, 'message'>): string {\n return toast.show({ message, intent: 'warning', ...options });\n },\n dismiss(id: string) {\n toasts = toasts.filter(t => t.id !== id);\n emit();\n },\n dismissAll() {\n toasts = [];\n emit();\n },\n};\n\n/* ─── useToasts hook ────────────────────────────────────────────────────────── */\n\nexport function useToasts(): ToastData[] {\n return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n}\n\n/* ─── Single Toast ──────────────────────────────────────────────────────────── */\n\nconst INTENT_MAP: Record<ToastIntent, SurfaceIntent> = {\n neutral: 'neutral',\n brand: 'brand',\n positive: 'positive',\n negative: 'negative',\n warning: 'warning',\n caution: 'caution',\n};\n\ninterface ToastItemProps {\n data: ToastData;\n}\n\nconst ToastItem: React.FC<ToastItemProps> = ({ data }) => {\n const [exiting, setExiting] = useState(false);\n const timerRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);\n\n const dismiss = useCallback(() => {\n setExiting(true);\n setTimeout(() => {\n toast.dismiss(data.id);\n data.onDismiss?.();\n }, 200);\n }, [data]);\n\n useEffect(() => {\n const duration = data.duration ?? 4000;\n if (duration > 0) {\n timerRef.current = setTimeout(dismiss, duration);\n }\n return () => { if (timerRef.current) clearTimeout(timerRef.current); };\n }, [data.duration, dismiss]);\n\n return (\n <Surface\n intent={INTENT_MAP[data.intent ?? 'neutral']}\n contrast=\"bold\"\n elevation=\"floating\"\n radius=\"lg\"\n className={cn(styles.toast, exiting && styles.toastExit)}\n role=\"status\"\n aria-live=\"polite\"\n >\n <Text variant=\"text-body-medium\" as=\"span\" color=\"inherit\">\n {data.message}\n </Text>\n <button\n type=\"button\"\n className={styles.dismiss}\n onClick={dismiss}\n aria-label=\"Dismiss\"\n >\n &times;\n </button>\n </Surface>\n );\n};\n\n/* ─── ToastContainer ────────────────────────────────────────────────────────── */\n\nexport interface ToastContainerProps {\n /** Where to render toasts on screen. */\n position?: ToastPosition;\n}\n\nexport const ToastContainer = forwardRef<HTMLDivElement, ToastContainerProps>(\n ({ position = 'top-center' }, ref) => {\n const items = useToasts();\n\n if (typeof document === 'undefined') return null;\n\n return ReactDOM.createPortal(\n <div\n ref={ref}\n className={cn(styles.container, styles[positionClass(position)])}\n aria-label=\"Notifications\"\n >\n {items.map(item => (\n <ToastItem key={item.id} data={item} />\n ))}\n </div>,\n document.body\n );\n }\n);\n\nToastContainer.displayName = 'ToastContainer';\n\nfunction positionClass(position: ToastPosition): string {\n switch (position) {\n case 'top-center': return 'topCenter';\n case 'top-right': return 'topRight';\n case 'bottom-center': return 'bottomCenter';\n case 'bottom-right': return 'bottomRight';\n }\n}\n",
74
74
  "type": "registry:ui"
75
75
  }
76
76
  ]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ds-mo/ui",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "description": "CompoMo — composable React UI components styled with TokoMo design tokens",
5
5
  "keywords": [
6
6
  "react",
@@ -0,0 +1,143 @@
1
+ import { Meta, Title, Subtitle, Canvas, Controls, Primary } from '@storybook/addon-docs/blocks';
2
+ import * as ButtonStories from './Button.stories';
3
+
4
+ <Meta of={ButtonStories} />
5
+
6
+ <Title />
7
+
8
+ <Subtitle>
9
+ Buttons trigger actions. They are the primary way users commit to a decision —
10
+ saving a record, opening a dialog, navigating somewhere meaningful.
11
+ </Subtitle>
12
+
13
+ A button should make its purpose obvious from its label, its visual weight, and
14
+ its position on the page. The right button in the right place is invisible — it
15
+ just feels like the next step. The wrong button competes for attention and slows
16
+ the user down.
17
+
18
+ ## Playground
19
+
20
+ Use the controls below to explore every prop the component accepts. This is the
21
+ fastest way to understand how `variant`, `intent`, `contrast`, and `elevation`
22
+ combine.
23
+
24
+ <Primary />
25
+
26
+ <Controls />
27
+
28
+ ## When to use
29
+
30
+ Reach for a button when the user needs to perform an **action** — submit a form,
31
+ delete a record, open a panel, run a process. If the user is navigating between
32
+ pages or sections, prefer a link instead. Buttons are for verbs; links are for
33
+ nouns.
34
+
35
+ A page should have **one** clearly dominant button. If two actions look equally
36
+ important, the user has to stop and read both before deciding — which is exactly
37
+ the moment of friction good hierarchy avoids.
38
+
39
+ ## Variants
40
+
41
+ `primary` is filled and carries the strongest visual weight. Use it for the one
42
+ action you most want the user to take on a given screen.
43
+
44
+ `secondary` is a softer treatment for actions that matter but should not compete
45
+ with the primary action. Use `elevation="none"` to get the ghost treatment for
46
+ tertiary actions like cancel, dismiss, or inline controls.
47
+
48
+ <Canvas of={ButtonStories.Variants} />
49
+
50
+ > **Do** pair one `primary` with one or more `secondary` buttons in a form footer.
51
+ >
52
+ > **Don't** stack two `primary` buttons next to each other — only one action can
53
+ > be the primary one.
54
+
55
+ ## Intent
56
+
57
+ Intent communicates the **meaning** of the action through color. Most buttons
58
+ should use `brand` (the default) or `neutral`. Reserve semantic intents for
59
+ moments when the meaning genuinely matches.
60
+
61
+ <Canvas of={ButtonStories.Intents} />
62
+
63
+ - `brand` — the default for primary calls-to-action
64
+ - `neutral` — for actions that should not draw color, like filters or toolbar controls
65
+ - `negative` — for destructive actions (delete, remove, discard)
66
+ - `positive` — for confirmations of a successful state, used sparingly
67
+ - `warning` / `caution` — for actions that need a moment of pause
68
+ - `ai` — reserved for AI-generated or AI-triggering actions
69
+ - `none` — inverted styling for use on colored or dark surfaces
70
+
71
+ > **Don't** use `negative` intent for a cancel button. Cancel is not destructive;
72
+ > it is the absence of an action. Use `secondary` with no intent.
73
+
74
+ ## Sizes
75
+
76
+ Pick a size from the surrounding density, not the importance of the action.
77
+ Forms and dialogs use `md`. Compact toolbars and table rows use `sm` or `xs`.
78
+ Hero sections and marketing surfaces can use `lg`.
79
+
80
+ <Canvas of={ButtonStories.Sizes} />
81
+
82
+ ## Icons
83
+
84
+ A leading icon can clarify a button's purpose at a glance — but only when the
85
+ icon's meaning is unambiguous. If you have to think about which icon to pick,
86
+ the label alone is doing a better job.
87
+
88
+ <Canvas of={ButtonStories.IconAndLabel} />
89
+
90
+ Icon-only buttons must always have an `aria-label`. Without it, screen readers
91
+ announce nothing meaningful, and keyboard users lose all context.
92
+
93
+ <Canvas of={ButtonStories.IconOnly} />
94
+
95
+ ## States
96
+
97
+ Buttons have several states that communicate progress or availability:
98
+
99
+ - `inactive` — the action is not currently available, but may become available
100
+ - `loading` — the action is in flight; prevents double-submission
101
+ - `rounded` — pill-shaped variant for filter chips and compact actions
102
+
103
+ <Canvas of={ButtonStories.Loading} />
104
+
105
+ <Canvas of={ButtonStories.Inactive} />
106
+
107
+ > **Do** use `loading` for any async action that takes longer than ~200ms.
108
+ > Without it, users will click again, often submitting twice.
109
+
110
+ ## Full width
111
+
112
+ Use `fullWidth` inside narrow containers like mobile sheets, side panels, or
113
+ form footers where the button should fill the available width. Avoid full-width
114
+ buttons on wide desktop layouts — they read as banner CTAs and feel heavy.
115
+
116
+ <Canvas of={ButtonStories.FullWidth} />
117
+
118
+ ## On colored surfaces
119
+
120
+ When a button sits on a colored or dark surface, set the `background` prop so
121
+ the button can adapt its contrast to remain legible. The `BackgroundContext`
122
+ story below demonstrates every variant against every supported surface.
123
+
124
+ <Canvas of={ButtonStories.BackgroundContext} />
125
+
126
+ ## Accessibility
127
+
128
+ - Every button must have a discernible name — either visible text in `label` or
129
+ an `aria-label` for icon-only buttons.
130
+ - The `inactive` state is **not** the same as the HTML `disabled` attribute.
131
+ `inactive` keeps the button focusable and announceable; truly disabled buttons
132
+ vanish from the tab order and frustrate users who can't tell why an action
133
+ isn't working.
134
+ - Touch targets meet the 24×24 CSS pixel minimum at all sizes; `xs` is intended
135
+ for dense desktop UIs and should not be used on mobile.
136
+
137
+ ## Full reference matrix
138
+
139
+ The matrix below renders every meaningful combination of `variant`, `intent`,
140
+ `contrast`, `elevation`, `size`, and state on a single canvas. Use it as a
141
+ visual regression reference when changing tokens or styles.
142
+
143
+ <Canvas of={ButtonStories.Matrix} />