@charcoal-ui/react 6.0.0-rc.0 → 6.0.0-rc.2

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 (47) hide show
  1. package/dist/components/Carousel/CarouselItem.d.ts +10 -0
  2. package/dist/components/Carousel/CarouselItem.d.ts.map +1 -0
  3. package/dist/components/Carousel/carouselStore.d.ts +25 -0
  4. package/dist/components/Carousel/carouselStore.d.ts.map +1 -0
  5. package/dist/components/Carousel/index.d.ts +61 -0
  6. package/dist/components/Carousel/index.d.ts.map +1 -0
  7. package/dist/components/Carousel/intersectionObserver.d.ts +7 -0
  8. package/dist/components/Carousel/intersectionObserver.d.ts.map +1 -0
  9. package/dist/components/Carousel/resizeObserver.d.ts +6 -0
  10. package/dist/components/Carousel/resizeObserver.d.ts.map +1 -0
  11. package/dist/components/Carousel/store.d.ts +7 -0
  12. package/dist/components/Carousel/store.d.ts.map +1 -0
  13. package/dist/components/Carousel/useCarouselScroller.d.ts +18 -0
  14. package/dist/components/Carousel/useCarouselScroller.d.ts.map +1 -0
  15. package/dist/components/Modal/index.d.ts +1 -1
  16. package/dist/components/TagItem/index.d.ts +5 -4
  17. package/dist/components/TagItem/index.d.ts.map +1 -1
  18. package/dist/index.cjs +2 -2
  19. package/dist/index.cjs.map +1 -1
  20. package/dist/index.css +275 -0
  21. package/dist/index.d.ts +3 -2
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +2 -2
  24. package/dist/index.js.map +1 -1
  25. package/dist/layered.css +275 -0
  26. package/dist/layered.css.map +1 -1
  27. package/package.json +5 -5
  28. package/src/components/Carousel/CarouselItem.tsx +56 -0
  29. package/src/components/Carousel/MIGRATION.md +117 -0
  30. package/src/components/Carousel/Migration.mdx +8 -0
  31. package/src/components/Carousel/__snapshots__/index.css.snap +274 -0
  32. package/src/components/Carousel/carouselStore.test.ts +38 -0
  33. package/src/components/Carousel/carouselStore.ts +48 -0
  34. package/src/components/Carousel/index.css +274 -0
  35. package/src/components/Carousel/index.story.tsx +169 -0
  36. package/src/components/Carousel/index.test.tsx +666 -0
  37. package/src/components/Carousel/index.tsx +278 -0
  38. package/src/components/Carousel/intersectionObserver.test.ts +68 -0
  39. package/src/components/Carousel/intersectionObserver.ts +58 -0
  40. package/src/components/Carousel/resizeObserver.test.ts +60 -0
  41. package/src/components/Carousel/resizeObserver.ts +35 -0
  42. package/src/components/Carousel/store.test.ts +25 -0
  43. package/src/components/Carousel/store.ts +27 -0
  44. package/src/components/Carousel/useCarouselScroller.ts +158 -0
  45. package/src/components/TagItem/__snapshots__/index.story.storyshot +17 -17
  46. package/src/components/TagItem/index.tsx +25 -7
  47. package/src/index.ts +14 -2
@@ -0,0 +1,158 @@
1
+ import {
2
+ useCallback,
3
+ useEffect,
4
+ useLayoutEffect,
5
+ useRef,
6
+ type RefObject,
7
+ } from 'react'
8
+ import type { CarouselStore } from './carouselStore'
9
+ import type { ScrollAlign, ScrollStep } from './index'
10
+
11
+ const useIsomorphicLayoutEffect =
12
+ typeof window !== 'undefined' ? useLayoutEffect : useEffect
13
+
14
+ const INTERACTION_EVENTS = ['pointerdown', 'wheel', 'touchstart'] as const
15
+
16
+ export type CarouselScrollerOptions = Readonly<{
17
+ align: ScrollAlign
18
+ offset: number
19
+ scrollStep: ScrollStep
20
+ onScroll?: (left: number) => void
21
+ onResize?: (width: number) => void
22
+ onScrollStateChange?: (canScroll: boolean) => void
23
+ }>
24
+
25
+ export type CarouselScrollerResult = Readonly<{
26
+ scrollByStep: (direction: 'prev' | 'next') => void
27
+ onItemResize: () => void
28
+ resetScroll: () => void
29
+ }>
30
+
31
+ export function useCarouselScroller(
32
+ scrollerRef: RefObject<HTMLElement | null>,
33
+ store: CarouselStore,
34
+ itemCount: number,
35
+ options: CarouselScrollerOptions,
36
+ ): CarouselScrollerResult {
37
+ const { align, offset, scrollStep, onScroll, onResize, onScrollStateChange } =
38
+ options
39
+ const initialScrollActive = useRef(true)
40
+
41
+ // コールバックは最新参照を ref に保持し、リスナーの貼り直しを避ける。
42
+ const callbacksRef = useRef({ onScroll, onResize, onScrollStateChange })
43
+ useEffect(() => {
44
+ callbacksRef.current = { onScroll, onResize, onScrollStateChange }
45
+ })
46
+
47
+ // onScrollStateChange は canScroll(=canPrev||canNext) が変化した時だけ発火する。
48
+ const prevCanScroll = useRef<boolean | null>(null)
49
+
50
+ const updateScrollState = useCallback(() => {
51
+ const el = scrollerRef.current
52
+ if (!el) return
53
+ const { scrollLeft, scrollWidth, clientWidth } = el
54
+ const canPrev = scrollLeft > 1
55
+ const canNext = scrollLeft < scrollWidth - clientWidth - 1
56
+ store.dispatch({ type: 'setScrollState', canPrev, canNext })
57
+ const canScroll = canPrev || canNext
58
+ if (prevCanScroll.current !== canScroll) {
59
+ prevCanScroll.current = canScroll
60
+ callbacksRef.current.onScrollStateChange?.(canScroll)
61
+ }
62
+ }, [scrollerRef, store])
63
+
64
+ const applyInitialScroll = useCallback(() => {
65
+ const el = scrollerRef.current
66
+ if (!el || !initialScrollActive.current) return
67
+ const maxScroll = el.scrollWidth - el.clientWidth
68
+ let left = offset
69
+ switch (align) {
70
+ case 'center':
71
+ left = maxScroll / 2 + offset
72
+ break
73
+ case 'right':
74
+ left = maxScroll + offset
75
+ break
76
+ }
77
+ // eslint-disable-next-line react-compiler/react-compiler
78
+ el.scrollLeft = Math.max(0, Math.min(left, maxScroll))
79
+ }, [scrollerRef, align, offset])
80
+
81
+ // canPrev/canNext: scroll で更新。onScroll もここから発火。itemCount 変化で貼り直し。
82
+ useIsomorphicLayoutEffect(() => {
83
+ const el = scrollerRef.current
84
+ if (!el) return
85
+ updateScrollState()
86
+ const handleScroll = () => {
87
+ updateScrollState()
88
+ callbacksRef.current.onScroll?.(el.scrollLeft)
89
+ }
90
+ el.addEventListener('scroll', handleScroll, { passive: true })
91
+ return () => el.removeEventListener('scroll', handleScroll)
92
+ }, [scrollerRef, updateScrollState, itemCount])
93
+
94
+ // scroller 幅の変化で onResize(clientWidth) を通知し、状態と初期位置を再計算する。
95
+ useIsomorphicLayoutEffect(() => {
96
+ const el = scrollerRef.current
97
+ if (!el || typeof ResizeObserver === 'undefined') return
98
+ const ro = new ResizeObserver(() => {
99
+ applyInitialScroll()
100
+ updateScrollState()
101
+ callbacksRef.current.onResize?.(el.clientWidth)
102
+ })
103
+ ro.observe(el)
104
+ return () => ro.disconnect()
105
+ }, [scrollerRef, applyInitialScroll, updateScrollState])
106
+
107
+ // 初期スクロール適用 + ユーザー操作で打ち切り。
108
+ useIsomorphicLayoutEffect(() => {
109
+ initialScrollActive.current = true
110
+ applyInitialScroll()
111
+ // 初期位置適用後の scrollLeft で canPrev/canNext を確定させる
112
+ // (center/right 初期化で scroll イベント待ちにならないように)。
113
+ updateScrollState()
114
+ const el = scrollerRef.current
115
+ if (!el) return
116
+ const stop = () => {
117
+ initialScrollActive.current = false
118
+ }
119
+ for (const type of INTERACTION_EVENTS) el.addEventListener(type, stop, true)
120
+ return () => {
121
+ for (const type of INTERACTION_EVENTS)
122
+ el.removeEventListener(type, stop, true)
123
+ }
124
+ }, [scrollerRef, applyInitialScroll, updateScrollState, itemCount])
125
+
126
+ const onItemResize = useCallback(() => {
127
+ applyInitialScroll()
128
+ updateScrollState()
129
+ }, [applyInitialScroll, updateScrollState])
130
+
131
+ // defaultScroll の初期位置へ戻す(命令的 API: CarouselHandlerRef.resetScroll)。
132
+ const resetScroll = useCallback(() => {
133
+ initialScrollActive.current = true
134
+ applyInitialScroll()
135
+ updateScrollState()
136
+ }, [applyInitialScroll, updateScrollState])
137
+
138
+ const scrollByStep = useCallback(
139
+ (direction: 'prev' | 'next') => {
140
+ const el = scrollerRef.current
141
+ if (!el) return
142
+ initialScrollActive.current = false
143
+ const { clientWidth, scrollWidth, scrollLeft } = el
144
+ // 進む量(px)の絶対値。符号は direction で付ける。
145
+ const delta =
146
+ typeof scrollStep === 'function'
147
+ ? scrollStep({ clientWidth, scrollWidth, scrollLeft, direction })
148
+ : clientWidth * scrollStep
149
+ el.scrollBy({
150
+ left: direction === 'next' ? delta : -delta,
151
+ behavior: 'smooth',
152
+ })
153
+ },
154
+ [scrollerRef, scrollStep],
155
+ )
156
+
157
+ return { scrollByStep, onItemResize, resetScroll }
158
+ }
@@ -5,7 +5,7 @@ exports[`Storybook Tests > react/TagItem > Active 1`] = `
5
5
  <div
6
6
  data-dark="false"
7
7
  >
8
- <button
8
+ <a
9
9
  class="charcoal-tag-item charcoal-tag-item__bg"
10
10
  data-bg-variant="color"
11
11
  data-size="M"
@@ -28,7 +28,7 @@ exports[`Storybook Tests > react/TagItem > Active 1`] = `
28
28
  name="16/Remove"
29
29
  style="--charcoal-icon-size: 16px;"
30
30
  />
31
- </button>
31
+ </a>
32
32
  </div>
33
33
  </div>
34
34
  `;
@@ -38,7 +38,7 @@ exports[`Storybook Tests > react/TagItem > BGColor 1`] = `
38
38
  <div
39
39
  data-dark="false"
40
40
  >
41
- <button
41
+ <a
42
42
  class="charcoal-tag-item charcoal-tag-item__bg"
43
43
  data-bg-variant="color"
44
44
  data-size="M"
@@ -56,7 +56,7 @@ exports[`Storybook Tests > react/TagItem > BGColor 1`] = `
56
56
  女の子
57
57
  </span>
58
58
  </div>
59
- </button>
59
+ </a>
60
60
  </div>
61
61
  </div>
62
62
  `;
@@ -66,7 +66,7 @@ exports[`Storybook Tests > react/TagItem > BGImage 1`] = `
66
66
  <div
67
67
  data-dark="false"
68
68
  >
69
- <button
69
+ <a
70
70
  class="charcoal-tag-item charcoal-tag-item__bg"
71
71
  data-bg-variant="image"
72
72
  data-size="M"
@@ -84,7 +84,7 @@ exports[`Storybook Tests > react/TagItem > BGImage 1`] = `
84
84
  #女の子
85
85
  </span>
86
86
  </div>
87
- </button>
87
+ </a>
88
88
  </div>
89
89
  </div>
90
90
  `;
@@ -94,7 +94,7 @@ exports[`Storybook Tests > react/TagItem > Default 1`] = `
94
94
  <div
95
95
  data-dark="false"
96
96
  >
97
- <button
97
+ <a
98
98
  class="charcoal-tag-item charcoal-tag-item__bg"
99
99
  data-bg-variant="color"
100
100
  data-size="M"
@@ -112,7 +112,7 @@ exports[`Storybook Tests > react/TagItem > Default 1`] = `
112
112
  #女の子
113
113
  </span>
114
114
  </div>
115
- </button>
115
+ </a>
116
116
  </div>
117
117
  </div>
118
118
  `;
@@ -122,12 +122,12 @@ exports[`Storybook Tests > react/TagItem > Disabled 1`] = `
122
122
  <div
123
123
  data-dark="false"
124
124
  >
125
- <button
125
+ <a
126
+ aria-disabled="true"
126
127
  class="charcoal-tag-item charcoal-tag-item__bg"
127
128
  data-bg-variant="color"
128
129
  data-size="M"
129
130
  data-state="default"
130
- disabled=""
131
131
  style="--charcoal-tag-item-bg: #7ACCB1;"
132
132
  >
133
133
  <div
@@ -141,7 +141,7 @@ exports[`Storybook Tests > react/TagItem > Disabled 1`] = `
141
141
  #女の子
142
142
  </span>
143
143
  </div>
144
- </button>
144
+ </a>
145
145
  </div>
146
146
  </div>
147
147
  `;
@@ -151,7 +151,7 @@ exports[`Storybook Tests > react/TagItem > InActive 1`] = `
151
151
  <div
152
152
  data-dark="false"
153
153
  >
154
- <button
154
+ <a
155
155
  class="charcoal-tag-item charcoal-tag-item__bg"
156
156
  data-bg-variant="color"
157
157
  data-size="M"
@@ -169,7 +169,7 @@ exports[`Storybook Tests > react/TagItem > InActive 1`] = `
169
169
  #女の子
170
170
  </span>
171
171
  </div>
172
- </button>
172
+ </a>
173
173
  </div>
174
174
  </div>
175
175
  `;
@@ -179,7 +179,7 @@ exports[`Storybook Tests > react/TagItem > Small 1`] = `
179
179
  <div
180
180
  data-dark="false"
181
181
  >
182
- <button
182
+ <a
183
183
  class="charcoal-tag-item charcoal-tag-item__bg"
184
184
  data-bg-variant="color"
185
185
  data-size="S"
@@ -197,7 +197,7 @@ exports[`Storybook Tests > react/TagItem > Small 1`] = `
197
197
  #女の子
198
198
  </span>
199
199
  </div>
200
- </button>
200
+ </a>
201
201
  </div>
202
202
  </div>
203
203
  `;
@@ -207,7 +207,7 @@ exports[`Storybook Tests > react/TagItem > TranslatedLabel 1`] = `
207
207
  <div
208
208
  data-dark="false"
209
209
  >
210
- <button
210
+ <a
211
211
  class="charcoal-tag-item charcoal-tag-item__bg"
212
212
  data-bg-variant="color"
213
213
  data-size="M"
@@ -230,7 +230,7 @@ exports[`Storybook Tests > react/TagItem > TranslatedLabel 1`] = `
230
230
  #女の子
231
231
  </span>
232
232
  </div>
233
- </button>
233
+ </a>
234
234
  </div>
235
235
  </div>
236
236
  `;
@@ -4,27 +4,29 @@ import { useClassNames } from '../../_lib/useClassNames'
4
4
  import './index.css'
5
5
 
6
6
  import { useObjectRef } from 'react-aria/useObjectRef'
7
+ import { useLink } from 'react-aria'
7
8
 
8
9
  type SizeMap = {
9
10
  S: 32
10
11
  M: 40
11
12
  }
12
13
 
13
- export type TagItemProps<T extends React.ElementType = 'button'> = {
14
+ export type TagItemProps<T extends React.ElementType = 'a'> = {
14
15
  label: string
15
16
  translatedLabel?: string
16
17
  bgColor?: string
17
18
  bgImage?: string
18
19
  status?: 'default' | 'active' | 'inactive'
19
20
  size?: keyof SizeMap
21
+ disabled?: boolean
20
22
  /**
21
23
  * The component used for root element.
22
- * @type T extends React.ElementType = 'button'
24
+ * @type T extends React.ElementType = 'a'
23
25
  */
24
26
  component?: T
25
- } & Omit<React.ComponentPropsWithRef<T>, 'children'>
27
+ } & Omit<React.ComponentPropsWithRef<T>, 'children' | 'disabled'>
26
28
 
27
- const TagItem = forwardRef<HTMLButtonElement, TagItemProps>(
29
+ const TagItem = forwardRef<HTMLAnchorElement, TagItemProps>(
28
30
  function TagItemInner<T extends React.ElementType>(
29
31
  {
30
32
  component,
@@ -34,9 +36,11 @@ const TagItem = forwardRef<HTMLButtonElement, TagItemProps>(
34
36
  bgImage,
35
37
  size = 'M',
36
38
  status = 'default',
39
+ disabled,
40
+ 'aria-disabled': ariaDisabled,
37
41
  ...props
38
42
  }: TagItemProps<T>,
39
- _ref: ForwardedRef<HTMLButtonElement>,
43
+ _ref: ForwardedRef<HTMLAnchorElement>,
40
44
  ) {
41
45
  const ref = useObjectRef(_ref)
42
46
 
@@ -52,11 +56,25 @@ const TagItem = forwardRef<HTMLButtonElement, TagItemProps>(
52
56
  bgImage !== undefined && bgImage.length > 0 ? 'image' : 'color'
53
57
  const bg = bgVariant === 'color' ? bgColor : `url(${bgImage ?? ''})`
54
58
 
55
- const Component = useMemo(() => component ?? 'button', [component])
59
+ const Component = useMemo(() => component ?? 'a', [component])
60
+ const isButton = Component === 'button'
61
+
62
+ const { linkProps } = useLink(
63
+ {
64
+ isDisabled: disabled,
65
+ elementType: typeof Component === 'string' ? Component : 'a',
66
+ },
67
+ ref,
68
+ )
69
+
70
+ const disabledProps = isButton
71
+ ? { disabled, 'aria-disabled': ariaDisabled }
72
+ : linkProps
56
73
 
57
74
  return (
58
75
  <Component
59
76
  {...props}
77
+ {...disabledProps}
60
78
  ref={ref}
61
79
  className={className}
62
80
  data-state={status}
@@ -84,6 +102,6 @@ const TagItem = forwardRef<HTMLButtonElement, TagItemProps>(
84
102
  </Component>
85
103
  )
86
104
  },
87
- ) as <T extends React.ElementType = 'button'>(p: TagItemProps<T>) => JSX.Element
105
+ ) as <T extends React.ElementType = 'a'>(p: TagItemProps<T>) => JSX.Element
88
106
 
89
107
  export default memo(TagItem)
package/src/index.ts CHANGED
@@ -88,11 +88,23 @@ export {
88
88
  type HintTextContext,
89
89
  } from './components/HintText'
90
90
  export {
91
- default as UnstableTextEllipsis,
91
+ default as TextEllipsis,
92
92
  type TextEllipsisProps,
93
93
  } from './components/TextEllipsis'
94
94
  export {
95
- default as UnstablePagination,
95
+ default as Pagination,
96
96
  type PaginationProps,
97
97
  } from './components/Pagination'
98
+ export {
99
+ default as Carousel,
100
+ type CarouselProps,
101
+ type CarouselItem,
102
+ type CarouselHandlerRef,
103
+ type ScrollAlign,
104
+ type ScrollSnap,
105
+ type ScrollSnapType,
106
+ type ScrollSnapAlign,
107
+ type ScrollStep,
108
+ type ScrollStepContext,
109
+ } from './components/Carousel'
98
110
  import './components/FocusRing/index.css'