@charcoal-ui/react 6.0.0-rc.1 → 6.0.0-rc.3
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/components/Carousel/CarouselItem.d.ts +10 -0
- package/dist/components/Carousel/CarouselItem.d.ts.map +1 -0
- package/dist/components/Carousel/carouselStore.d.ts +25 -0
- package/dist/components/Carousel/carouselStore.d.ts.map +1 -0
- package/dist/components/Carousel/index.d.ts +61 -0
- package/dist/components/Carousel/index.d.ts.map +1 -0
- package/dist/components/Carousel/intersectionObserver.d.ts +7 -0
- package/dist/components/Carousel/intersectionObserver.d.ts.map +1 -0
- package/dist/components/Carousel/resizeObserver.d.ts +6 -0
- package/dist/components/Carousel/resizeObserver.d.ts.map +1 -0
- package/dist/components/Carousel/store.d.ts +7 -0
- package/dist/components/Carousel/store.d.ts.map +1 -0
- package/dist/components/Carousel/useCarouselScroller.d.ts +18 -0
- package/dist/components/Carousel/useCarouselScroller.d.ts.map +1 -0
- package/dist/components/Icon/index.d.ts +1 -1
- package/dist/components/Icon/index.d.ts.map +1 -1
- package/dist/index.cjs +2 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.css +319 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/layered.css +319 -1
- package/dist/layered.css.map +1 -1
- package/package.json +5 -5
- package/src/__tests__/fixtures/legacy-v1-index.css +1836 -0
- package/src/__tests__/token-v1-compat.test.ts +118 -0
- package/src/__tests__/token-v1-remap.test.ts +133 -0
- package/src/components/Carousel/CarouselItem.tsx +56 -0
- package/src/components/Carousel/MIGRATION.md +117 -0
- package/src/components/Carousel/Migration.mdx +8 -0
- package/src/components/Carousel/__snapshots__/index.css.snap +274 -0
- package/src/components/Carousel/carouselStore.test.ts +38 -0
- package/src/components/Carousel/carouselStore.ts +48 -0
- package/src/components/Carousel/index.css +274 -0
- package/src/components/Carousel/index.story.tsx +169 -0
- package/src/components/Carousel/index.test.tsx +666 -0
- package/src/components/Carousel/index.tsx +278 -0
- package/src/components/Carousel/intersectionObserver.test.ts +68 -0
- package/src/components/Carousel/intersectionObserver.ts +58 -0
- package/src/components/Carousel/resizeObserver.test.ts +60 -0
- package/src/components/Carousel/resizeObserver.ts +35 -0
- package/src/components/Carousel/store.test.ts +25 -0
- package/src/components/Carousel/store.ts +27 -0
- package/src/components/Carousel/useCarouselScroller.ts +158 -0
- package/src/components/Icon/index.tsx +2 -1
- package/src/components/Modal/__snapshots__/index.css.snap +1 -1
- package/src/components/Modal/index.css +1 -1
- package/src/index.ts +12 -0
|
@@ -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
|
+
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import * as React from 'react'
|
|
2
2
|
|
|
3
3
|
import '@charcoal-ui/icons'
|
|
4
|
-
|
|
4
|
+
// tmp fix for https://github.com/rolldown/tsdown/pull/981
|
|
5
|
+
import '../../../../icons/css/icon.css'
|
|
5
6
|
import { calcActualSize } from '@charcoal-ui/icons'
|
|
6
7
|
import type { IconSizing, PixivIcon, Props } from '@charcoal-ui/icons'
|
|
7
8
|
|
package/src/index.ts
CHANGED
|
@@ -95,4 +95,16 @@ export {
|
|
|
95
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'
|