@charcoal-ui/react 6.0.1 → 6.1.0-beta.1

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.
Files changed (60) hide show
  1. package/dist/components/Notification/NotificationItem.cjs +30 -0
  2. package/dist/components/Notification/NotificationItem.cjs.map +1 -0
  3. package/dist/components/Notification/NotificationItem.d.ts +15 -0
  4. package/dist/components/Notification/NotificationItem.d.ts.map +1 -0
  5. package/dist/components/Notification/NotificationItem.js +30 -0
  6. package/dist/components/Notification/NotificationItem.js.map +1 -0
  7. package/dist/components/Notification/NotificationRegion.cjs +50 -0
  8. package/dist/components/Notification/NotificationRegion.cjs.map +1 -0
  9. package/dist/components/Notification/NotificationRegion.d.ts +14 -0
  10. package/dist/components/Notification/NotificationRegion.d.ts.map +1 -0
  11. package/dist/components/Notification/NotificationRegion.js +50 -0
  12. package/dist/components/Notification/NotificationRegion.js.map +1 -0
  13. package/dist/components/Notification/Snackbar/index.d.ts +45 -0
  14. package/dist/components/Notification/Snackbar/index.d.ts.map +1 -0
  15. package/dist/components/Notification/Snackbar/index2.cjs +99 -0
  16. package/dist/components/Notification/Snackbar/index2.cjs.map +1 -0
  17. package/dist/components/Notification/Snackbar/index2.js +94 -0
  18. package/dist/components/Notification/Snackbar/index2.js.map +1 -0
  19. package/dist/components/Notification/Toast/index.d.ts +28 -0
  20. package/dist/components/Notification/Toast/index.d.ts.map +1 -0
  21. package/dist/components/Notification/Toast/index2.cjs +86 -0
  22. package/dist/components/Notification/Toast/index2.cjs.map +1 -0
  23. package/dist/components/Notification/Toast/index2.js +81 -0
  24. package/dist/components/Notification/Toast/index2.js.map +1 -0
  25. package/dist/components/Notification/types.d.ts +27 -0
  26. package/dist/components/Notification/types.d.ts.map +1 -0
  27. package/dist/components/Notification/useNotificationQueue.cjs +155 -0
  28. package/dist/components/Notification/useNotificationQueue.cjs.map +1 -0
  29. package/dist/components/Notification/useNotificationQueue.d.ts +16 -0
  30. package/dist/components/Notification/useNotificationQueue.d.ts.map +1 -0
  31. package/dist/components/Notification/useNotificationQueue.js +155 -0
  32. package/dist/components/Notification/useNotificationQueue.js.map +1 -0
  33. package/dist/components/Pagination/index2.js +52 -60
  34. package/dist/components/Pagination/index2.js.map +1 -1
  35. package/dist/index.cjs +6 -0
  36. package/dist/index.css +1608 -1432
  37. package/dist/index.d.ts +3 -0
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +3 -1
  40. package/dist/layered.css +1608 -1432
  41. package/dist/layered.css.map +1 -1
  42. package/package.json +5 -5
  43. package/src/__tests__/css-output.test.ts +39 -6
  44. package/src/components/Notification/NotificationItem.tsx +51 -0
  45. package/src/components/Notification/NotificationRegion.tsx +76 -0
  46. package/src/components/Notification/Snackbar/__snapshots__/index.css.snap +69 -0
  47. package/src/components/Notification/Snackbar/index.css +68 -0
  48. package/src/components/Notification/Snackbar/index.story.tsx +213 -0
  49. package/src/components/Notification/Snackbar/index.test.tsx +344 -0
  50. package/src/components/Notification/Snackbar/index.tsx +131 -0
  51. package/src/components/Notification/Toast/__snapshots__/index.css.snap +62 -0
  52. package/src/components/Notification/Toast/index.css +61 -0
  53. package/src/components/Notification/Toast/index.story.tsx +174 -0
  54. package/src/components/Notification/Toast/index.test.tsx +263 -0
  55. package/src/components/Notification/Toast/index.tsx +86 -0
  56. package/src/components/Notification/__snapshots__/layout.css.snap +47 -0
  57. package/src/components/Notification/layout.css +46 -0
  58. package/src/components/Notification/types.ts +28 -0
  59. package/src/components/Notification/useNotificationQueue.ts +235 -0
  60. package/src/index.ts +19 -0
@@ -0,0 +1,86 @@
1
+ import '../layout.css'
2
+ import './index.css'
3
+
4
+ import { forwardRef, useImperativeHandle, useRef } from 'react'
5
+ import type { ReactNode } from 'react'
6
+ import { NotificationItem } from '../NotificationItem'
7
+ import { NotificationRegion } from '../NotificationRegion'
8
+ import { useNotificationQueue } from '../useNotificationQueue'
9
+ import type { NotificationProps, Position } from '../types'
10
+
11
+ export type ToastType = 'success' | 'error'
12
+
13
+ export type ShowToastOptions = {
14
+ type: ToastType
15
+ }
16
+
17
+ export type ToastHandler = {
18
+ show: (message: ReactNode, options: ShowToastOptions) => void
19
+ }
20
+
21
+ export type ToastProps = Omit<NotificationProps, 'position'> & {
22
+ /**
23
+ * Toast の表示位置
24
+ * @default 'top'
25
+ */
26
+ position?: Position
27
+ }
28
+
29
+ type ToastContent = {
30
+ message: ReactNode
31
+ type: ToastType
32
+ }
33
+
34
+ const Toast = forwardRef<ToastHandler, ToastProps>(function Toast(
35
+ { position = 'top', duration, order, ...regionProps },
36
+ ref,
37
+ ) {
38
+ 'use memo'
39
+
40
+ const { state, itemRef, enqueue, onHoverStart, onHoverEnd } =
41
+ useNotificationQueue<ToastContent>('toast', { duration, order })
42
+
43
+ function show(message: ReactNode, options: ShowToastOptions) {
44
+ enqueue({
45
+ message,
46
+ type: options.type,
47
+ })
48
+ }
49
+
50
+ useImperativeHandle(ref, () => ({ show }))
51
+
52
+ return (
53
+ <NotificationRegion
54
+ name="toast"
55
+ state={state}
56
+ position={position}
57
+ {...regionProps}
58
+ >
59
+ {state.visibleToasts.map((toast) => (
60
+ <NotificationItem
61
+ key={toast.key}
62
+ name="toast"
63
+ toast={toast}
64
+ state={state}
65
+ itemRef={itemRef}
66
+ onHoverStart={onHoverStart}
67
+ onHoverEnd={onHoverEnd}
68
+ data-type={toast.content.type}
69
+ />
70
+ ))}
71
+ </NotificationRegion>
72
+ )
73
+ })
74
+
75
+ export default Toast
76
+
77
+ export function useToast(props: ToastProps = {}) {
78
+ 'use memo'
79
+
80
+ const toastHandlerRef = useRef<ToastHandler>(null)
81
+ const element = <Toast ref={toastHandlerRef} {...props} />
82
+ function show(message: ReactNode, options: ShowToastOptions) {
83
+ toastHandlerRef.current?.show(message, options)
84
+ }
85
+ return [element, show] as const
86
+ }
@@ -0,0 +1,47 @@
1
+ .charcoal-notification-region {
2
+ position: fixed;
3
+ left: 0;
4
+ right: 0;
5
+ display: grid;
6
+ justify-content: center;
7
+ pointer-events: none;
8
+ box-sizing: border-box;
9
+ }
10
+
11
+ .charcoal-notification {
12
+ box-sizing: border-box;
13
+ border-radius: var(--charcoal-radius-oval);
14
+ width: fit-content;
15
+ max-width: calc(100vw - var(--charcoal-space-46));
16
+ display: flex;
17
+ align-items: center;
18
+ justify-content: center;
19
+ gap: var(--charcoal-space-20);
20
+ pointer-events: auto;
21
+ }
22
+
23
+ .charcoal-notification-content {
24
+ display: flex;
25
+ align-items: center;
26
+ min-width: 0;
27
+ }
28
+
29
+ .charcoal-notification-label {
30
+ min-width: 0;
31
+ overflow: hidden;
32
+ overflow-wrap: break-word;
33
+ display: -webkit-box;
34
+ -webkit-box-orient: vertical;
35
+ -webkit-line-clamp: 2;
36
+ font-size: var(--charcoal-text-font-size-caption-m);
37
+ font-weight: var(--charcoal-text-font-weight-bold);
38
+ line-height: var(--charcoal-text-line-height-caption-m);
39
+ }
40
+
41
+ @media (prefers-reduced-motion: reduce) {
42
+
43
+ .charcoal-notification, .charcoal-notification[data-exiting='true'] {
44
+ animation: none;
45
+ }
46
+ }
47
+
@@ -0,0 +1,46 @@
1
+ .charcoal-notification-region {
2
+ position: fixed;
3
+ left: 0;
4
+ right: 0;
5
+ display: grid;
6
+ justify-content: center;
7
+ pointer-events: none;
8
+ box-sizing: border-box;
9
+ }
10
+
11
+ .charcoal-notification {
12
+ box-sizing: border-box;
13
+ border-radius: var(--charcoal-radius-oval);
14
+ width: fit-content;
15
+ max-width: calc(100vw - var(--charcoal-space-46));
16
+ display: flex;
17
+ align-items: center;
18
+ justify-content: center;
19
+ gap: var(--charcoal-space-20);
20
+ pointer-events: auto;
21
+ }
22
+
23
+ .charcoal-notification-content {
24
+ display: flex;
25
+ align-items: center;
26
+ min-width: 0;
27
+ }
28
+
29
+ .charcoal-notification-label {
30
+ min-width: 0;
31
+ overflow: hidden;
32
+ overflow-wrap: break-word;
33
+ display: -webkit-box;
34
+ -webkit-box-orient: vertical;
35
+ -webkit-line-clamp: 2;
36
+ font-size: var(--charcoal-text-font-size-caption-m);
37
+ font-weight: var(--charcoal-text-font-weight-bold);
38
+ line-height: var(--charcoal-text-line-height-caption-m);
39
+ }
40
+
41
+ @media (prefers-reduced-motion: reduce) {
42
+ .charcoal-notification,
43
+ .charcoal-notification[data-exiting='true'] {
44
+ animation: none;
45
+ }
46
+ }
@@ -0,0 +1,28 @@
1
+ export type NotificationName = 'snackbar' | 'toast'
2
+ export type Position = 'top' | 'bottom'
3
+ export type NotificationOrder = 'queue' | 'replace'
4
+
5
+ export type UseNotificationOptions = {
6
+ position?: Position
7
+ /**
8
+ * 画面端からの距離(ピクセル)
9
+ * @default 16
10
+ */
11
+ offset?: number
12
+ /**
13
+ * 通知を表示する時間(ミリ秒)。負の値は 0、数値でない値は 5000 として扱う
14
+ * @default 5000
15
+ */
16
+ duration?: number
17
+ /**
18
+ * 表示中の通知がある場合の次の通知の表示方法
19
+ * @default 'queue'
20
+ */
21
+ order?: NotificationOrder
22
+ }
23
+
24
+ export type NotificationProps = UseNotificationOptions & {
25
+ zIndex?: number
26
+ portalContainer?: HTMLElement
27
+ className?: string
28
+ }
@@ -0,0 +1,235 @@
1
+ import { useCallback, useEffect, useRef } from 'react'
2
+ import { useToastState } from 'react-stately/useToastState'
3
+ import type { NotificationName, NotificationOrder } from './types'
4
+
5
+ const DEFAULT_DURATION_MS = 5000
6
+ const ANIMATION_DURATION_MS = 300
7
+
8
+ type QueueItem<TContent, TCloseReason> = {
9
+ content: TContent
10
+ onClose?: (reason: TCloseReason) => void
11
+ }
12
+
13
+ export function useNotificationQueue<
14
+ TContent,
15
+ TCloseReason extends string = never,
16
+ >(
17
+ name: NotificationName,
18
+ {
19
+ duration: durationOption,
20
+ order = 'queue',
21
+ timeoutReason,
22
+ unmountedReason,
23
+ }: {
24
+ duration?: number
25
+ order?: NotificationOrder
26
+ timeoutReason?: TCloseReason
27
+ unmountedReason?: TCloseReason
28
+ } = {},
29
+ ) {
30
+ const itemRef = useRef<HTMLDivElement>(null)
31
+ const queueRef = useRef<
32
+ Array<QueueItem<TContent, TCloseReason> & { duration: number }>
33
+ >([])
34
+ const isShowingRef = useRef(false)
35
+ const activeRef = useRef<{
36
+ key: string
37
+ onClose?: (reason: TCloseReason) => void
38
+ reason?: TCloseReason
39
+ notified: boolean
40
+ }>()
41
+ const replaceRef = useRef(false)
42
+ const hoverRef = useRef({
43
+ active: false,
44
+ pending: undefined as (() => void) | undefined,
45
+ })
46
+ // wrapUpdate を固定したまま、後から定義する playNext の最新版を呼ぶ
47
+ const playNextRef = useRef<(() => void) | undefined>(undefined)
48
+
49
+ // wrapUpdate の参照が変わると useToastState が ToastQueue を作り直すため useCallback で固定する
50
+ const wrapUpdate = useCallback(
51
+ function wrapUpdate(
52
+ update: () => void,
53
+ action: 'add' | 'remove' | 'clear',
54
+ ) {
55
+ if (action !== 'remove') {
56
+ update()
57
+ return
58
+ }
59
+
60
+ function notifyActive() {
61
+ const active = activeRef.current
62
+ if (active === undefined || active.notified) return
63
+ active.notified = true
64
+ const reason = active.reason ?? timeoutReason
65
+ if (reason !== undefined) {
66
+ active.onClose?.(reason)
67
+ }
68
+ }
69
+
70
+ function finish() {
71
+ activeRef.current = undefined
72
+ update()
73
+ playNextRef.current?.()
74
+ }
75
+
76
+ notifyActive()
77
+
78
+ if (hoverRef.current.active) {
79
+ hoverRef.current.pending = () => wrapUpdate(update, 'remove')
80
+ return
81
+ }
82
+
83
+ if (itemRef.current === null) {
84
+ finish()
85
+ return
86
+ }
87
+
88
+ if (replaceRef.current) {
89
+ replaceRef.current = false
90
+ finish()
91
+ return
92
+ }
93
+ const item: HTMLDivElement = itemRef.current
94
+
95
+ // allow-discreteが Newly Available で使えないので、要素の削除をアニメーション完了まで待つ
96
+ item.dataset.exiting = 'true'
97
+ let completed = false
98
+ let fallbackTimer = 0 // complete 内で clearTimeout(setTimeout(...)) すると新規タイマーを即キャンセルしてしまう
99
+ function handleAnimationEnd(event: AnimationEvent) {
100
+ if (
101
+ event.target === item &&
102
+ event.animationName === `charcoal-${name}-exit`
103
+ ) {
104
+ complete()
105
+ }
106
+ }
107
+ function complete() {
108
+ if (completed) return
109
+ completed = true
110
+ item.removeEventListener('animationend', handleAnimationEnd)
111
+ window.clearTimeout(fallbackTimer)
112
+ finish()
113
+ }
114
+ item.addEventListener('animationend', handleAnimationEnd)
115
+ fallbackTimer = window.setTimeout(complete, ANIMATION_DURATION_MS + 100)
116
+ if (window.matchMedia?.('(prefers-reduced-motion: reduce)').matches) {
117
+ complete()
118
+ }
119
+ },
120
+ [name, timeoutReason],
121
+ )
122
+
123
+ const state = useToastState<TContent>({
124
+ maxVisibleToasts: 1,
125
+ wrapUpdate,
126
+ })
127
+
128
+ useEffect(() => {
129
+ return () => {
130
+ const active = activeRef.current
131
+ if (active !== undefined && !active.notified) {
132
+ if (unmountedReason !== undefined) {
133
+ active.onClose?.(unmountedReason)
134
+ }
135
+ }
136
+ activeRef.current = undefined
137
+ queueRef.current = []
138
+ isShowingRef.current = false
139
+ }
140
+ }, [unmountedReason])
141
+
142
+ function playNext() {
143
+ const next = queueRef.current.shift()
144
+ if (next === undefined) {
145
+ isShowingRef.current = false
146
+ return
147
+ }
148
+
149
+ const { duration, onClose, content } = next
150
+ isShowingRef.current = true
151
+ const enterMs = window.matchMedia?.('(prefers-reduced-motion: reduce)')
152
+ .matches
153
+ ? 0
154
+ : ANIMATION_DURATION_MS
155
+ const key = state.add(content, {
156
+ // react-stately は 0 を「タイマーなし」と扱うため、最小の正数を渡す
157
+ timeout: Math.max(1, duration + enterMs),
158
+ })
159
+ activeRef.current = { key, onClose, notified: false }
160
+ }
161
+ playNextRef.current = playNext
162
+
163
+ function enqueue(
164
+ content: TContent,
165
+ onClose?: (reason: TCloseReason) => void,
166
+ ) {
167
+ const duration =
168
+ typeof durationOption === 'number' && Number.isFinite(durationOption)
169
+ ? Math.max(0, durationOption)
170
+ : DEFAULT_DURATION_MS
171
+
172
+ queueRef.current.push({
173
+ content,
174
+ onClose,
175
+ duration,
176
+ })
177
+ if (!isShowingRef.current) {
178
+ playNext()
179
+ } else if (order === 'replace') {
180
+ const active = activeRef.current
181
+ if (active === undefined) return
182
+
183
+ active.reason = 'replaced' as TCloseReason
184
+ replaceRef.current = true
185
+ hoverRef.current.active = false
186
+ const pending = hoverRef.current.pending
187
+ hoverRef.current.pending = undefined
188
+ if (pending !== undefined) {
189
+ pending()
190
+ } else {
191
+ state.close(active.key)
192
+ }
193
+ }
194
+ }
195
+
196
+ function close(key: string, reason: TCloseReason) {
197
+ const active = activeRef.current
198
+ if (active?.key !== key) return
199
+ active.reason = reason
200
+ hoverRef.current.active = false
201
+ const pending = hoverRef.current.pending
202
+ hoverRef.current.pending = undefined
203
+ if (pending !== undefined) {
204
+ pending()
205
+ } else {
206
+ state.close(key)
207
+ }
208
+ }
209
+
210
+ function onHoverStart() {
211
+ hoverRef.current.active = true
212
+ }
213
+
214
+ function onHoverEnd() {
215
+ hoverRef.current.active = false
216
+ const pending = hoverRef.current.pending
217
+ hoverRef.current.pending = undefined
218
+ pending?.()
219
+ }
220
+
221
+ function clearHover() {
222
+ hoverRef.current.active = false
223
+ hoverRef.current.pending = undefined
224
+ }
225
+
226
+ return {
227
+ state,
228
+ itemRef,
229
+ enqueue,
230
+ close,
231
+ onHoverStart,
232
+ onHoverEnd,
233
+ clearHover,
234
+ }
235
+ }
package/src/index.ts CHANGED
@@ -106,4 +106,23 @@ export {
106
106
  type ScrollStep,
107
107
  type ScrollStepContext,
108
108
  } from './components/Carousel'
109
+ export {
110
+ default as unstable_Snackbar,
111
+ useSnackbar as unstable_useSnackbar,
112
+ type SnackbarProps as unstable_SnackbarProps,
113
+ type SnackbarHandler as unstable_SnackbarHandler,
114
+ type SnackbarCloseReason as unstable_SnackbarCloseReason,
115
+ type ShowSnackbarOptions as unstable_ShowSnackbarOptions,
116
+ } from './components/Notification/Snackbar'
117
+ export {
118
+ default as unstable_Toast,
119
+ useToast as unstable_useToast,
120
+ type ToastProps as unstable_ToastProps,
121
+ type ToastHandler as unstable_ToastHandler,
122
+ type ShowToastOptions as unstable_ShowToastOptions,
123
+ } from './components/Notification/Toast'
124
+ export {
125
+ type NotificationOrder as unstable_NotificationOrder,
126
+ type UseNotificationOptions as unstable_UseNotificationOptions,
127
+ } from './components/Notification/types'
109
128
  import './components/FocusRing/index.css'