@yamada-ui/react 2.2.1-dev-20260428144932 → 2.2.1-dev-20260428151511
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/cjs/components/carousel/use-carousel.cjs +9 -8
- package/dist/cjs/components/carousel/use-carousel.cjs.map +1 -1
- package/dist/cjs/components/tabs/use-tabs.cjs +10 -18
- package/dist/cjs/components/tabs/use-tabs.cjs.map +1 -1
- package/dist/esm/components/carousel/use-carousel.js +9 -8
- package/dist/esm/components/carousel/use-carousel.js.map +1 -1
- package/dist/esm/components/tabs/use-tabs.js +10 -18
- package/dist/esm/components/tabs/use-tabs.js.map +1 -1
- package/dist/types/components/accordion/accordion.d.ts +2 -2
- package/dist/types/components/accordion/accordion.style.d.ts +1 -1
- package/dist/types/components/accordion/use-accordion.d.ts +11 -11
- package/dist/types/components/action-bar/action-bar.d.ts +2 -2
- package/dist/types/components/airy/airy.d.ts +2 -2
- package/dist/types/components/alert/alert.d.ts +2 -2
- package/dist/types/components/alpha-slider/alpha-slider.d.ts +2 -2
- package/dist/types/components/aspect-ratio/aspect-ratio.d.ts +2 -2
- package/dist/types/components/autocomplete/autocomplete.d.ts +2 -2
- package/dist/types/components/autocomplete/autocomplete.style.d.ts +1 -1
- package/dist/types/components/autocomplete/use-autocomplete.d.ts +5 -5
- package/dist/types/components/avatar/avatar.d.ts +5 -5
- package/dist/types/components/avatar/avatar.style.d.ts +1 -1
- package/dist/types/components/avatar/use-avatar-group.d.ts +2 -2
- package/dist/types/components/badge/badge.d.ts +2 -2
- package/dist/types/components/bleed/bleed.d.ts +2 -2
- package/dist/types/components/blockquote/blockquote.d.ts +2 -2
- package/dist/types/components/breadcrumb/breadcrumb.d.ts +2 -2
- package/dist/types/components/breadcrumb/breadcrumb.style.d.ts +1 -1
- package/dist/types/components/breadcrumb/use-breadcrumb.d.ts +2 -2
- package/dist/types/components/button/button.d.ts +2 -2
- package/dist/types/components/button/icon-button.d.ts +2 -2
- package/dist/types/components/calendar/calendar.d.ts +2 -2
- package/dist/types/components/calendar/calendar.style.d.ts +2 -2
- package/dist/types/components/calendar/use-calendar.d.ts +8 -8
- package/dist/types/components/card/card.d.ts +2 -2
- package/dist/types/components/carousel/carousel.d.ts +2 -2
- package/dist/types/components/carousel/carousel.style.d.ts +2 -2
- package/dist/types/components/carousel/use-carousel.d.ts +3 -3
- package/dist/types/components/center/center.d.ts +2 -2
- package/dist/types/components/chart/area-chart.d.ts +2 -2
- package/dist/types/components/chart/bar-chart.d.ts +2 -2
- package/dist/types/components/chart/chart.d.ts +5 -5
- package/dist/types/components/chart/composed-chart.d.ts +2 -2
- package/dist/types/components/chart/donut-chart.d.ts +2 -2
- package/dist/types/components/chart/line-chart.d.ts +2 -2
- package/dist/types/components/chart/pie-chart.d.ts +2 -2
- package/dist/types/components/chart/radar-chart.d.ts +2 -2
- package/dist/types/components/chart/use-chart.d.ts +2 -2
- package/dist/types/components/checkbox/checkbox.d.ts +5 -5
- package/dist/types/components/checkbox/checkbox.style.d.ts +1 -1
- package/dist/types/components/checkbox/use-checkbox-group.d.ts +2 -2
- package/dist/types/components/checkbox-card/checkbox-card.d.ts +5 -5
- package/dist/types/components/checkbox-card/checkbox-card.style.d.ts +1 -1
- package/dist/types/components/circle-progress/circle-progress.d.ts +2 -2
- package/dist/types/components/close-button/close-button.d.ts +2 -2
- package/dist/types/components/code/code.d.ts +2 -2
- package/dist/types/components/collapse/collapse.d.ts +2 -2
- package/dist/types/components/color-picker/color-picker.d.ts +2 -2
- package/dist/types/components/color-picker/use-color-picker.d.ts +2 -2
- package/dist/types/components/color-selector/color-selector.d.ts +2 -2
- package/dist/types/components/color-selector/use-color-selector.d.ts +3 -3
- package/dist/types/components/color-swatch/color-swatch.d.ts +2 -2
- package/dist/types/components/container/container.d.ts +2 -2
- package/dist/types/components/data-list/data-list.d.ts +2 -2
- package/dist/types/components/data-list/data-list.style.d.ts +1 -1
- package/dist/types/components/date-picker/date-picker.d.ts +2 -2
- package/dist/types/components/date-picker/use-date-picker.d.ts +4 -4
- package/dist/types/components/drawer/drawer.d.ts +2 -2
- package/dist/types/components/dropzone/dropzone.d.ts +4 -4
- package/dist/types/components/editable/editable.d.ts +4 -4
- package/dist/types/components/editable/use-editable.d.ts +2 -2
- package/dist/types/components/em/em.d.ts +2 -2
- package/dist/types/components/empty-state/empty-state.d.ts +2 -2
- package/dist/types/components/empty-state/empty-state.style.d.ts +2 -2
- package/dist/types/components/fade/fade.d.ts +2 -2
- package/dist/types/components/field/field.d.ts +3 -3
- package/dist/types/components/field/use-field-props.d.ts +3 -3
- package/dist/types/components/fieldset/fieldset.d.ts +3 -3
- package/dist/types/components/file-button/file-button.d.ts +2 -2
- package/dist/types/components/file-button/use-file-button.d.ts +18 -18
- package/dist/types/components/file-input/file-input.d.ts +2 -2
- package/dist/types/components/file-input/use-file-input.d.ts +15 -15
- package/dist/types/components/flex/flex.d.ts +2 -2
- package/dist/types/components/flip/flip.d.ts +4 -4
- package/dist/types/components/flip/flip.style.d.ts +1 -1
- package/dist/types/components/float/float.d.ts +2 -2
- package/dist/types/components/form/form.d.ts +3 -3
- package/dist/types/components/format/format-byte.d.ts +4 -4
- package/dist/types/components/format/format-date-time.d.ts +4 -4
- package/dist/types/components/format/format-number.d.ts +2 -2
- package/dist/types/components/grid/grid-item.d.ts +2 -2
- package/dist/types/components/grid/grid.d.ts +2 -2
- package/dist/types/components/group/group.d.ts +2 -2
- package/dist/types/components/group/use-group.d.ts +2 -2
- package/dist/types/components/heading/heading.d.ts +2 -2
- package/dist/types/components/hue-slider/hue-slider.d.ts +3 -3
- package/dist/types/components/icon/icon.d.ts +5 -5
- package/dist/types/components/image/image.d.ts +2 -2
- package/dist/types/components/indicator/indicator.d.ts +4 -4
- package/dist/types/components/infinite-scroll-area/infinite-scroll-area.d.ts +2 -2
- package/dist/types/components/input/input-addon.d.ts +2 -2
- package/dist/types/components/input/input-element.d.ts +2 -2
- package/dist/types/components/input/input.d.ts +2 -2
- package/dist/types/components/kbd/kbd.d.ts +2 -2
- package/dist/types/components/link/link.d.ts +2 -2
- package/dist/types/components/link-box/link-box.d.ts +2 -2
- package/dist/types/components/list/list.d.ts +2 -2
- package/dist/types/components/list/list.style.d.ts +2 -2
- package/dist/types/components/loading/loading-provider.d.ts +2 -2
- package/dist/types/components/loading/loading.d.ts +3 -3
- package/dist/types/components/mark/mark.d.ts +2 -2
- package/dist/types/components/menu/menu.d.ts +2 -2
- package/dist/types/components/menu/use-menu.d.ts +11 -11
- package/dist/types/components/modal/modal.d.ts +2 -2
- package/dist/types/components/native-accordion/native-accordion.d.ts +2 -2
- package/dist/types/components/native-accordion/native-accordion.style.d.ts +2 -2
- package/dist/types/components/native-accordion/use-native-accordion.d.ts +2 -2
- package/dist/types/components/native-popover/native-popover.d.ts +2 -2
- package/dist/types/components/native-select/native-select.d.ts +2 -2
- package/dist/types/components/native-table/native-table.d.ts +4 -4
- package/dist/types/components/notice/notice.style.d.ts +1 -1
- package/dist/types/components/number-input/number-input.d.ts +2 -2
- package/dist/types/components/pagination/pagination.d.ts +2 -2
- package/dist/types/components/pagination/pagination.style.d.ts +1 -1
- package/dist/types/components/pagination/use-pagination.d.ts +2 -2
- package/dist/types/components/password-input/password-input.d.ts +2 -2
- package/dist/types/components/password-input/strength-meter.d.ts +2 -2
- package/dist/types/components/password-input/strength-meter.style.d.ts +1 -1
- package/dist/types/components/password-input/use-password-input.d.ts +2 -2
- package/dist/types/components/pin-input/pin-input.d.ts +2 -2
- package/dist/types/components/pin-input/use-pin-input.d.ts +7 -7
- package/dist/types/components/popover/popover.d.ts +2 -2
- package/dist/types/components/progress/progress.d.ts +4 -4
- package/dist/types/components/progress/use-progress.d.ts +706 -706
- package/dist/types/components/qr-code/qr-code.d.ts +2 -2
- package/dist/types/components/radio/radio.d.ts +5 -5
- package/dist/types/components/radio/radio.style.d.ts +1 -1
- package/dist/types/components/radio/use-radio-group.d.ts +2 -2
- package/dist/types/components/radio-card/radio-card.d.ts +5 -5
- package/dist/types/components/radio-card/radio-card.style.d.ts +1 -1
- package/dist/types/components/rating/rating.style.d.ts +2 -2
- package/dist/types/components/rating/use-rating.d.ts +7 -7
- package/dist/types/components/reorder/reorder.d.ts +2 -2
- package/dist/types/components/reorder/reorder.style.d.ts +1 -1
- package/dist/types/components/reorder/use-reorder.d.ts +3 -3
- package/dist/types/components/resizable/resizable.d.ts +2 -2
- package/dist/types/components/resizable/resizable.style.d.ts +2 -2
- package/dist/types/components/resizable/use-resizable.d.ts +2 -2
- package/dist/types/components/ripple/ripple.d.ts +2 -2
- package/dist/types/components/ripple/use-ripple.d.ts +2 -2
- package/dist/types/components/rotate/rotate.d.ts +2 -2
- package/dist/types/components/saturation-slider/saturation-slider.d.ts +2 -2
- package/dist/types/components/saturation-slider/use-saturation-slider.d.ts +2 -2
- package/dist/types/components/scroll-area/scroll-area.d.ts +2 -2
- package/dist/types/components/segmented-control/segmented-control.d.ts +2 -2
- package/dist/types/components/segmented-control/segmented-control.style.d.ts +2 -2
- package/dist/types/components/segmented-control/use-segmented-control.d.ts +9 -9
- package/dist/types/components/select/select.d.ts +2 -2
- package/dist/types/components/select/select.style.d.ts +1 -1
- package/dist/types/components/select/use-select.d.ts +4 -4
- package/dist/types/components/separator/separator.d.ts +2 -2
- package/dist/types/components/sidebar/sidebar.d.ts +2 -2
- package/dist/types/components/sidebar/use-sidebar.d.ts +9 -9
- package/dist/types/components/simple-grid/simple-grid.d.ts +2 -2
- package/dist/types/components/skeleton/skeleton.d.ts +2 -2
- package/dist/types/components/slide/slide.d.ts +2 -2
- package/dist/types/components/slide-fade/slide-fade.d.ts +2 -2
- package/dist/types/components/slider/slider.d.ts +2 -2
- package/dist/types/components/slider/use-slider.d.ts +2 -2
- package/dist/types/components/stack/h-stack.d.ts +2 -2
- package/dist/types/components/stack/stack.d.ts +2 -2
- package/dist/types/components/stack/v-stack.d.ts +2 -2
- package/dist/types/components/stack/z-stack.d.ts +2 -2
- package/dist/types/components/stat/stat.d.ts +2 -2
- package/dist/types/components/status/status.d.ts +2 -2
- package/dist/types/components/status/status.style.d.ts +2 -2
- package/dist/types/components/steps/steps.d.ts +2 -2
- package/dist/types/components/steps/steps.style.d.ts +1 -1
- package/dist/types/components/steps/use-steps.d.ts +10 -10
- package/dist/types/components/switch/switch.d.ts +2 -2
- package/dist/types/components/table/table.d.ts +2 -2
- package/dist/types/components/tabs/tabs.d.ts +2 -2
- package/dist/types/components/tabs/use-tabs.d.ts +16 -16
- package/dist/types/components/tag/tag.d.ts +2 -2
- package/dist/types/components/text/text.d.ts +2 -2
- package/dist/types/components/textarea/textarea.d.ts +2 -2
- package/dist/types/components/textarea/use-autosize.d.ts +2 -2
- package/dist/types/components/timeline/timeline.d.ts +5 -5
- package/dist/types/components/timeline/timeline.style.d.ts +1 -1
- package/dist/types/components/toggle/toggle.d.ts +5 -5
- package/dist/types/components/toggle/use-toggle-group.d.ts +2 -2
- package/dist/types/components/toggle/use-toggle.d.ts +2 -2
- package/dist/types/components/tooltip/tooltip.d.ts +2 -2
- package/dist/types/components/tree/tree.d.ts +2 -2
- package/dist/types/components/tree/tree.style.d.ts +1 -1
- package/dist/types/components/tree/use-tree.d.ts +182 -182
- package/dist/types/components/visually-hidden/visually-hidden.d.ts +2 -2
- package/dist/types/components/wrap/wrap.d.ts +2 -2
- package/dist/types/core/components/create-component.d.ts +7 -7
- package/dist/types/core/system/color-mode-provider.d.ts +2 -2
- package/dist/types/core/system/styled.d.ts +2 -2
- package/dist/types/core/system/system-provider.d.ts +2 -2
- package/dist/types/hooks/use-breakpoint/use-breakpoint-state.d.ts +2 -2
- package/dist/types/hooks/use-clipboard/index.d.ts +2 -2
- package/dist/types/hooks/use-combobox/index.d.ts +12 -12
- package/dist/types/hooks/use-counter/index.d.ts +2 -2
- package/dist/types/hooks/use-descendants/index.d.ts +2 -2
- package/dist/types/providers/i18n-provider/i18n-provider.d.ts +3 -3
- package/dist/types/providers/ui-provider/ui-provider.d.ts +2 -2
- package/package.json +2 -2
|
@@ -6,6 +6,8 @@ const require_dom = require('../../utils/dom.cjs');
|
|
|
6
6
|
const require_effect = require('../../utils/effect.cjs');
|
|
7
7
|
const require_ref = require('../../utils/ref.cjs');
|
|
8
8
|
const require_utils_index = require('../../utils/index.cjs');
|
|
9
|
+
const require_props = require('../../core/components/props.cjs');
|
|
10
|
+
require('../../core/index.cjs');
|
|
9
11
|
const require_hooks_use_controllable_state_index = require('../../hooks/use-controllable-state/index.cjs');
|
|
10
12
|
const require_i18n_provider = require('../../providers/i18n-provider/i18n-provider.cjs');
|
|
11
13
|
require('../../providers/i18n-provider/index.cjs');
|
|
@@ -171,16 +173,15 @@ const useCarousel = ({ id, align = "center", autoplay = false, containScroll = f
|
|
|
171
173
|
skipSnaps,
|
|
172
174
|
slidesToScroll
|
|
173
175
|
]);
|
|
174
|
-
const getRootProps = (0, react.useCallback)(({ ref, ...props } = {}) => ({
|
|
176
|
+
const getRootProps = (0, react.useCallback)(({ ref, ...props } = {}) => require_props.mergeProps({
|
|
175
177
|
id,
|
|
176
178
|
"aria-roledescription": "carousel",
|
|
177
|
-
"data-orientation": orientation
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
}), [
|
|
179
|
+
"data-orientation": orientation
|
|
180
|
+
}, rest, {
|
|
181
|
+
ref,
|
|
182
|
+
onMouseEnter,
|
|
183
|
+
onMouseLeave
|
|
184
|
+
}, props)(), [
|
|
184
185
|
id,
|
|
185
186
|
onMouseEnter,
|
|
186
187
|
onMouseLeave,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-carousel.cjs","names":["createContext","useI18n","useControllableState","useIds","useBoolean","snapCount","total","index","getRootProps: PropGetter<\"section\">","mergeRefs","getListProps: PropGetter","getItemProps: RequiredPropGetter<\"div\", { index: number }>","indexProp","getPrevTriggerProps: PropGetter<\"button\">","getNextTriggerProps: PropGetter<\"button\">","getIndicatorsProps: PropGetter"],"sources":["../../../../src/components/carousel/use-carousel.ts"],"sourcesContent":["\"use client\"\n\nimport type {\n EmblaCarouselType,\n EmblaOptionsType,\n EmblaPluginType,\n} from \"embla-carousel\"\nimport type { KeyboardEvent, RefObject } from \"react\"\nimport type {\n HTMLProps,\n Orientation,\n PropGetter,\n RequiredPropGetter,\n} from \"../../core\"\nimport useEmblaCarousel from \"embla-carousel-react\"\nimport { useCallback, useEffect, useRef, useState } from \"react\"\nimport { useBoolean } from \"../../hooks/use-boolean\"\nimport { useControllableState } from \"../../hooks/use-controllable-state\"\nimport { useI18n } from \"../../providers/i18n-provider\"\nimport {\n assignRef,\n createContext,\n dataAttr,\n handlerAll,\n isFunction,\n mergeRefs,\n runKeyAction,\n useIds,\n useUpdateEffect,\n} from \"../../utils\"\n\ntype EmblaOptions = Required<EmblaOptionsType>\nexport type CarouselAlign = EmblaOptions[\"align\"]\nexport type CarouselContainScroll = EmblaOptions[\"containScroll\"]\nexport type CarouselInViewThreshold = EmblaOptions[\"inViewThreshold\"]\nexport type CarouselWatchDrag = EmblaOptions[\"watchDrag\"]\nexport type CarouselWatchResize = EmblaOptions[\"watchResize\"]\nexport type CarouselWatchSlides = EmblaOptions[\"watchSlides\"]\nexport type CarouselControl = EmblaCarouselType\nexport type CarouselPlugin = EmblaPluginType\n\nexport interface CarouselContext extends Omit<\n UseCarouselReturn,\n \"getRootProps\"\n> {}\n\nconst [CarouselContext, useCarouselContext] = createContext<CarouselContext>({\n name: \"CarouselContext\",\n})\n\nexport { CarouselContext, useCarouselContext }\n\nexport interface UseCarouselProps extends Omit<\n HTMLProps<\"section\">,\n \"onChange\"\n> {\n /**\n * The alignment of the carousel.\n *\n * @default 'center'\n */\n align?: CarouselAlign\n /**\n * If `true`, the carousel will be autoplay.\n *\n * @default false\n */\n autoplay?: boolean\n /**\n * Clear leading and trailing empty space that causes excessive scrolling.\n * Use trimSnaps to only use snap points that trigger scrolling or keepSnaps to keep them.\n *\n * @default false\n */\n containScroll?: CarouselContainScroll\n /**\n * Ref of the resizable item callback.\n */\n controlRef?: RefObject<CarouselControl | null>\n /**\n * The initial index of the carousel slide.\n *\n * @default 0\n */\n defaultIndex?: number\n /**\n * The number for the autoplay interval of the carousel.\n *\n * @default 4000\n */\n delay?: number\n /**\n * If `true`, momentum scrolling will be enabled.\n *\n * @default false\n */\n dragFree?: boolean\n /**\n * If `true`, carousel can be scrolled with mouse and touch interactions.\n *\n * @default true\n */\n draggable?: boolean\n /**\n * Set scroll duration when triggered by any of the API methods.\n * Higher numbers enables slower scrolling.\n * Drag interactions are not affected because duration is then determined by the drag force.\n *\n * @default 25\n */\n duration?: number\n /**\n * The index of the carousel slide.\n */\n index?: number\n /**\n * Choose a fraction representing the percentage portion of a slide that needs to be visible in order to be considered in view.\n *\n * @default 0\n */\n inViewThreshold?: CarouselInViewThreshold\n /**\n * If `true`, infinite looping.\n * Automatically falls back to false if slide content isn't enough to loop.\n *\n * @default true\n */\n loop?: boolean\n /**\n * The orientation of the carousel.\n *\n * @default 'horizontal'\n */\n orientation?: Orientation\n /**\n * Embla plugins to use.\n */\n plugins?: CarouselPlugin[]\n /**\n * If `true`, allow the carousel to skip scroll snaps if it's dragged vigorously.\n * Note that this option will be ignored if the dragFree option is set to true.\n *\n * @default false\n */\n skipSnaps?: boolean\n /**\n * The number of slides that should be scrolled with next or previous buttons.\n *\n * @default 1\n */\n slidesToScroll?: number\n /**\n * If `true`, autoplay will pause when the mouse entries over.\n *\n * @default true\n */\n stopMouseEnterAutoplay?: boolean\n /**\n * Enables for scrolling the carousel with mouse and touch interactions.\n * Set this to `false` to disable drag events or pass a custom callback to add your own drag logic.\n *\n * @default true\n */\n watchDrag?: CarouselWatchDrag\n /**\n * Embla automatically watches the container and slides for size changes and runs `reInit` when any size has changed.\n * Set this to `false` to disable this behaviour or pass a custom callback to add your own resize logic.\n *\n * @default true\n */\n watchResize?: CarouselWatchResize\n /**\n * Embla automatically watches the container for added and/or removed slides and runs `reInit` if needed.\n * Set this to `false` to disable this behaviour or pass a custom callback to add your own slides changed logic.\n *\n * @default true\n */\n watchSlides?: CarouselWatchSlides\n /**\n * The callback invoked when carousel slide selected.\n */\n onChange?: (index: number) => void\n /**\n * A callback that return the current scroll amount when the carousel is scrolled.\n */\n onScrollProgress?: (progress: number) => void\n}\n\nexport const useCarousel = ({\n id,\n align = \"center\",\n autoplay = false,\n containScroll = false,\n controlRef,\n defaultIndex = 0,\n delay = 4000,\n dragFree = false,\n draggable = true,\n duration = 25,\n index: indexProp,\n inViewThreshold = 0,\n loop = true,\n orientation = \"horizontal\",\n plugins = [],\n skipSnaps = false,\n slidesToScroll = 1,\n stopMouseEnterAutoplay = true,\n watchDrag = draggable,\n watchResize: watchResizeProp = true,\n watchSlides = true,\n onChange,\n onScrollProgress,\n ...rest\n}: UseCarouselProps = {}) => {\n const { t } = useI18n(\"carousel\")\n const [index, setIndex] = useControllableState({\n defaultValue: defaultIndex,\n value: indexProp,\n onChange,\n })\n const [rootId, listId] = useIds()\n const [hover, { off: onMouseLeave, on: onMouseEnter }] = useBoolean()\n const timeoutId = useRef<NodeJS.Timeout | null>(null)\n const indicatorMapRef = useRef<Map<number, HTMLButtonElement | null>>(\n new Map(),\n )\n const listRef = useRef<HTMLDivElement>(null)\n const horizontal = orientation === \"horizontal\"\n const axis = horizontal ? \"x\" : \"y\"\n const [snapCount, setSnapCount] = useState(0)\n const [total, setTotal] = useState(0)\n const watchResize = useCallback<Extract<CarouselWatchResize, Function>>(\n (methods, entries) => {\n const result = isFunction(watchResizeProp)\n ? watchResizeProp(methods, entries)\n : true\n const snapCount = methods.scrollSnapList().length\n const total = methods.slideNodes().length\n\n setSnapCount(snapCount)\n setTotal(total)\n\n return result\n },\n [watchResizeProp],\n )\n const [carouselRef, carousel] = useEmblaCarousel(\n {\n align,\n axis,\n container: listRef.current,\n containScroll,\n dragFree,\n duration,\n inViewThreshold,\n loop,\n skipSnaps,\n slidesToScroll,\n startIndex: defaultIndex,\n watchDrag,\n watchResize,\n watchSlides,\n },\n plugins,\n )\n\n id ??= rootId\n\n const onInit = useCallback((methods: CarouselControl) => {\n const snapCount = methods.scrollSnapList().length\n const total = methods.slideNodes().length\n\n setSnapCount(snapCount)\n setTotal(total)\n }, [])\n\n const onScroll = useCallback(() => {\n if (!carousel) return\n\n const progress = Math.round(\n Math.max(0, Math.min(1, carousel.scrollProgress())) * 100,\n )\n\n onScrollProgress?.(progress)\n }, [carousel, onScrollProgress])\n\n const onSelect = useCallback(() => {\n if (!carousel) return\n\n const index = carousel.selectedScrollSnap()\n\n setIndex(index)\n }, [carousel, setIndex])\n\n const onFocusIndicator = useCallback(\n (index: number) => {\n const el = indicatorMapRef.current.get(index)\n\n el?.focus()\n carousel?.scrollTo(index)\n },\n [carousel],\n )\n\n const onKeyDown = useCallback(\n (index: number) => (ev: KeyboardEvent<HTMLButtonElement>) => {\n const lastIndex = snapCount - 1\n\n runKeyAction(ev, {\n ArrowDown: () => {\n if (horizontal) return\n\n index = index === lastIndex ? 0 : index + 1\n\n onFocusIndicator(index)\n },\n ArrowLeft: () => {\n if (!horizontal) return\n\n index = index === 0 ? lastIndex : index - 1\n\n onFocusIndicator(index)\n },\n ArrowRight: () => {\n if (!horizontal) return\n\n index = index === lastIndex ? 0 : index + 1\n\n onFocusIndicator(index)\n },\n ArrowUp: () => {\n if (horizontal) return\n\n index = index === 0 ? lastIndex : index - 1\n\n onFocusIndicator(index)\n },\n End: () => {\n onFocusIndicator(lastIndex)\n },\n Home: () => {\n onFocusIndicator(0)\n },\n })\n },\n [snapCount, horizontal, onFocusIndicator],\n )\n\n assignRef(controlRef, carousel)\n\n useEffect(() => {\n if (carousel) {\n carousel.on(\"reInit\", onInit)\n carousel.on(\"select\", onSelect)\n carousel.on(\"scroll\", onScroll)\n\n onScroll()\n\n return () => {\n carousel.off(\"reInit\", onInit)\n carousel.off(\"select\", onSelect)\n carousel.off(\"scroll\", onScroll)\n }\n }\n }, [carousel, onInit, onScroll, onSelect])\n\n useEffect(() => {\n const stop = hover && stopMouseEnterAutoplay\n const last = !carousel?.canScrollNext()\n\n if (carousel && autoplay && !stop && !last) {\n timeoutId.current = setInterval(() => {\n carousel.scrollNext()\n }, delay)\n } else {\n if (timeoutId.current) clearInterval(timeoutId.current)\n\n timeoutId.current = null\n }\n\n return () => {\n if (timeoutId.current) clearInterval(timeoutId.current)\n }\n }, [autoplay, carousel, delay, hover, stopMouseEnterAutoplay])\n\n useUpdateEffect(() => {\n if (!carousel) return\n\n if (indexProp === undefined) return\n\n carousel.scrollTo(indexProp)\n }, [indexProp])\n\n useUpdateEffect(() => {\n if (!carousel) return\n\n carousel.reInit()\n }, [\n carousel,\n total,\n align,\n axis,\n containScroll,\n dragFree,\n duration,\n inViewThreshold,\n loop,\n skipSnaps,\n slidesToScroll,\n ])\n\n const getRootProps: PropGetter<\"section\"> = useCallback(\n ({ ref, ...props } = {}) => ({\n id,\n \"aria-roledescription\": \"carousel\",\n \"data-orientation\": orientation,\n ...rest,\n ...props,\n ref: mergeRefs(ref, rest.ref),\n onMouseEnter: handlerAll(props.onMouseEnter, onMouseEnter),\n onMouseLeave: handlerAll(props.onMouseLeave, onMouseLeave),\n }),\n [id, onMouseEnter, onMouseLeave, rest, orientation],\n )\n\n const getListProps: PropGetter = useCallback(\n ({ ref, ...props } = {}) => ({\n id: listId,\n \"aria-live\": autoplay ? \"off\" : \"polite\",\n \"data-orientation\": orientation,\n ...props,\n ref: mergeRefs(ref, listRef, carouselRef),\n }),\n [autoplay, listId, orientation, carouselRef],\n )\n\n const getItemProps: RequiredPropGetter<\"div\", { index: number }> =\n useCallback(\n ({ index: indexProp, ...props }) => {\n const page = indexProp + 1\n const selected = index === indexProp\n\n return {\n id: `${listId}-${indexProp}`,\n \"aria-label\": t(\"{page} of {total}\", { page, total }),\n \"aria-roledescription\": \"slide\",\n \"data-index\": indexProp.toString(),\n \"data-orientation\": orientation,\n \"data-selected\": dataAttr(selected),\n role: \"tabpanel\",\n ...props,\n }\n },\n [index, listId, total, orientation, t],\n )\n\n const getPrevTriggerProps: PropGetter<\"button\"> = useCallback(\n (props = {}) => ({\n \"aria-controls\": listId,\n \"aria-label\": t(\"Go to previous slide\"),\n \"data-orientation\": orientation,\n disabled: !carousel?.canScrollPrev(),\n ...props,\n onClick: handlerAll(props.onClick, () => carousel?.scrollPrev()),\n }),\n [carousel, listId, orientation, t],\n )\n\n const getNextTriggerProps: PropGetter<\"button\"> = useCallback(\n (props = {}) => ({\n \"aria-controls\": listId,\n \"aria-label\": t(\"Go to next slide\"),\n \"data-orientation\": orientation,\n disabled: !carousel?.canScrollNext(),\n ...props,\n onClick: handlerAll(props.onClick, () => carousel?.scrollNext()),\n }),\n [carousel, listId, orientation, t],\n )\n\n const getIndicatorsProps: PropGetter = useCallback(\n (props = {}) => ({\n \"aria-label\": t(\"Slides\"),\n \"aria-orientation\": orientation,\n role: \"tablist\",\n ...props,\n }),\n [orientation, t],\n )\n\n const getIndicatorProps: RequiredPropGetter<\"button\", { index: number }> =\n useCallback(\n ({ ref, index: indexProp, ...props }) => {\n const page = indexProp + 1\n const selected = index === indexProp\n\n return {\n type: \"button\" as const,\n \"aria-controls\": `${listId}-${indexProp}`,\n \"aria-label\": t(\"Go to {page} slide\", { page }),\n \"aria-selected\": selected,\n \"data-index\": indexProp.toString(),\n \"data-orientation\": orientation,\n \"data-selected\": dataAttr(selected),\n role: \"tab\",\n tabIndex: selected ? 0 : -1,\n ...props,\n ref: mergeRefs(ref, (node) => {\n indicatorMapRef.current.set(indexProp, node)\n }),\n onClick: handlerAll(props.onClick, () =>\n carousel?.scrollTo(indexProp),\n ),\n onKeyDown: handlerAll(props.onKeyDown, onKeyDown(indexProp)),\n }\n },\n [index, listId, t, orientation, onKeyDown, carousel],\n )\n\n return {\n carousel,\n index,\n setIndex,\n snapCount,\n total,\n getIndicatorProps,\n getIndicatorsProps,\n getItemProps,\n getListProps,\n getNextTriggerProps,\n getPrevTriggerProps,\n getRootProps,\n }\n}\n\nexport type UseCarouselReturn = ReturnType<typeof useCarousel>\n"],"mappings":";;;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AiB,GAAG,OAAO,GAAGA;IAC9B,cAAc,EAAE,sBAAsB,EAAE,MAAM,CAAC;IAC/C,iBAAiB;IACjB,cAAcA,YAAU,UAAU;IAClC,oBAAoB;IACpB,iEAA0B,SAAS;IACnC,MAAM;IACN,UAAU,WAAW,IAAI;IACzB,GAAG;IACH,KAAKH,sBAAU,MAAM,SAAS;AAC5B,qBAAgB,QAAQ,IAAIG,aAAW,KAAK;MAC5C;IACF,2DAAoB,MAAM,eACxB,UAAU,SAASA,YAAU,CAC9B;IACD,6DAAsB,MAAM,WAAW,UAAUA,YAAU,CAAC;IAC7D;KAEH;GAAC;GAAO;GAAQ;GAAG;GAAa;GAAW;GAAS,CACrD;EASD;EACA;EACA;EACA;EACA;EACA;EACD"}
|
|
1
|
+
{"version":3,"file":"use-carousel.cjs","names":["createContext","useI18n","useControllableState","useIds","useBoolean","snapCount","total","index","getRootProps: PropGetter<\"section\">","mergeProps","getListProps: PropGetter","mergeRefs","getItemProps: RequiredPropGetter<\"div\", { index: number }>","indexProp","getPrevTriggerProps: PropGetter<\"button\">","getNextTriggerProps: PropGetter<\"button\">","getIndicatorsProps: PropGetter"],"sources":["../../../../src/components/carousel/use-carousel.ts"],"sourcesContent":["\"use client\"\n\nimport type {\n EmblaCarouselType,\n EmblaOptionsType,\n EmblaPluginType,\n} from \"embla-carousel\"\nimport type { KeyboardEvent, RefObject } from \"react\"\nimport type {\n HTMLProps,\n Orientation,\n PropGetter,\n RequiredPropGetter,\n} from \"../../core\"\nimport useEmblaCarousel from \"embla-carousel-react\"\nimport { useCallback, useEffect, useRef, useState } from \"react\"\nimport { mergeProps } from \"../../core\"\nimport { useBoolean } from \"../../hooks/use-boolean\"\nimport { useControllableState } from \"../../hooks/use-controllable-state\"\nimport { useI18n } from \"../../providers/i18n-provider\"\nimport {\n assignRef,\n createContext,\n dataAttr,\n handlerAll,\n isFunction,\n mergeRefs,\n runKeyAction,\n useIds,\n useUpdateEffect,\n} from \"../../utils\"\n\ntype EmblaOptions = Required<EmblaOptionsType>\nexport type CarouselAlign = EmblaOptions[\"align\"]\nexport type CarouselContainScroll = EmblaOptions[\"containScroll\"]\nexport type CarouselInViewThreshold = EmblaOptions[\"inViewThreshold\"]\nexport type CarouselWatchDrag = EmblaOptions[\"watchDrag\"]\nexport type CarouselWatchResize = EmblaOptions[\"watchResize\"]\nexport type CarouselWatchSlides = EmblaOptions[\"watchSlides\"]\nexport type CarouselControl = EmblaCarouselType\nexport type CarouselPlugin = EmblaPluginType\n\nexport interface CarouselContext extends Omit<\n UseCarouselReturn,\n \"getRootProps\"\n> {}\n\nconst [CarouselContext, useCarouselContext] = createContext<CarouselContext>({\n name: \"CarouselContext\",\n})\n\nexport { CarouselContext, useCarouselContext }\n\nexport interface UseCarouselProps extends Omit<\n HTMLProps<\"section\">,\n \"onChange\"\n> {\n /**\n * The alignment of the carousel.\n *\n * @default 'center'\n */\n align?: CarouselAlign\n /**\n * If `true`, the carousel will be autoplay.\n *\n * @default false\n */\n autoplay?: boolean\n /**\n * Clear leading and trailing empty space that causes excessive scrolling.\n * Use trimSnaps to only use snap points that trigger scrolling or keepSnaps to keep them.\n *\n * @default false\n */\n containScroll?: CarouselContainScroll\n /**\n * Ref of the resizable item callback.\n */\n controlRef?: RefObject<CarouselControl | null>\n /**\n * The initial index of the carousel slide.\n *\n * @default 0\n */\n defaultIndex?: number\n /**\n * The number for the autoplay interval of the carousel.\n *\n * @default 4000\n */\n delay?: number\n /**\n * If `true`, momentum scrolling will be enabled.\n *\n * @default false\n */\n dragFree?: boolean\n /**\n * If `true`, carousel can be scrolled with mouse and touch interactions.\n *\n * @default true\n */\n draggable?: boolean\n /**\n * Set scroll duration when triggered by any of the API methods.\n * Higher numbers enables slower scrolling.\n * Drag interactions are not affected because duration is then determined by the drag force.\n *\n * @default 25\n */\n duration?: number\n /**\n * The index of the carousel slide.\n */\n index?: number\n /**\n * Choose a fraction representing the percentage portion of a slide that needs to be visible in order to be considered in view.\n *\n * @default 0\n */\n inViewThreshold?: CarouselInViewThreshold\n /**\n * If `true`, infinite looping.\n * Automatically falls back to false if slide content isn't enough to loop.\n *\n * @default true\n */\n loop?: boolean\n /**\n * The orientation of the carousel.\n *\n * @default 'horizontal'\n */\n orientation?: Orientation\n /**\n * Embla plugins to use.\n */\n plugins?: CarouselPlugin[]\n /**\n * If `true`, allow the carousel to skip scroll snaps if it's dragged vigorously.\n * Note that this option will be ignored if the dragFree option is set to true.\n *\n * @default false\n */\n skipSnaps?: boolean\n /**\n * The number of slides that should be scrolled with next or previous buttons.\n *\n * @default 1\n */\n slidesToScroll?: number\n /**\n * If `true`, autoplay will pause when the mouse entries over.\n *\n * @default true\n */\n stopMouseEnterAutoplay?: boolean\n /**\n * Enables for scrolling the carousel with mouse and touch interactions.\n * Set this to `false` to disable drag events or pass a custom callback to add your own drag logic.\n *\n * @default true\n */\n watchDrag?: CarouselWatchDrag\n /**\n * Embla automatically watches the container and slides for size changes and runs `reInit` when any size has changed.\n * Set this to `false` to disable this behaviour or pass a custom callback to add your own resize logic.\n *\n * @default true\n */\n watchResize?: CarouselWatchResize\n /**\n * Embla automatically watches the container for added and/or removed slides and runs `reInit` if needed.\n * Set this to `false` to disable this behaviour or pass a custom callback to add your own slides changed logic.\n *\n * @default true\n */\n watchSlides?: CarouselWatchSlides\n /**\n * The callback invoked when carousel slide selected.\n */\n onChange?: (index: number) => void\n /**\n * A callback that return the current scroll amount when the carousel is scrolled.\n */\n onScrollProgress?: (progress: number) => void\n}\n\nexport const useCarousel = ({\n id,\n align = \"center\",\n autoplay = false,\n containScroll = false,\n controlRef,\n defaultIndex = 0,\n delay = 4000,\n dragFree = false,\n draggable = true,\n duration = 25,\n index: indexProp,\n inViewThreshold = 0,\n loop = true,\n orientation = \"horizontal\",\n plugins = [],\n skipSnaps = false,\n slidesToScroll = 1,\n stopMouseEnterAutoplay = true,\n watchDrag = draggable,\n watchResize: watchResizeProp = true,\n watchSlides = true,\n onChange,\n onScrollProgress,\n ...rest\n}: UseCarouselProps = {}) => {\n const { t } = useI18n(\"carousel\")\n const [index, setIndex] = useControllableState({\n defaultValue: defaultIndex,\n value: indexProp,\n onChange,\n })\n const [rootId, listId] = useIds()\n const [hover, { off: onMouseLeave, on: onMouseEnter }] = useBoolean()\n const timeoutId = useRef<NodeJS.Timeout | null>(null)\n const indicatorMapRef = useRef<Map<number, HTMLButtonElement | null>>(\n new Map(),\n )\n const listRef = useRef<HTMLDivElement>(null)\n const horizontal = orientation === \"horizontal\"\n const axis = horizontal ? \"x\" : \"y\"\n const [snapCount, setSnapCount] = useState(0)\n const [total, setTotal] = useState(0)\n const watchResize = useCallback<Extract<CarouselWatchResize, Function>>(\n (methods, entries) => {\n const result = isFunction(watchResizeProp)\n ? watchResizeProp(methods, entries)\n : true\n const snapCount = methods.scrollSnapList().length\n const total = methods.slideNodes().length\n\n setSnapCount(snapCount)\n setTotal(total)\n\n return result\n },\n [watchResizeProp],\n )\n const [carouselRef, carousel] = useEmblaCarousel(\n {\n align,\n axis,\n container: listRef.current,\n containScroll,\n dragFree,\n duration,\n inViewThreshold,\n loop,\n skipSnaps,\n slidesToScroll,\n startIndex: defaultIndex,\n watchDrag,\n watchResize,\n watchSlides,\n },\n plugins,\n )\n\n id ??= rootId\n\n const onInit = useCallback((methods: CarouselControl) => {\n const snapCount = methods.scrollSnapList().length\n const total = methods.slideNodes().length\n\n setSnapCount(snapCount)\n setTotal(total)\n }, [])\n\n const onScroll = useCallback(() => {\n if (!carousel) return\n\n const progress = Math.round(\n Math.max(0, Math.min(1, carousel.scrollProgress())) * 100,\n )\n\n onScrollProgress?.(progress)\n }, [carousel, onScrollProgress])\n\n const onSelect = useCallback(() => {\n if (!carousel) return\n\n const index = carousel.selectedScrollSnap()\n\n setIndex(index)\n }, [carousel, setIndex])\n\n const onFocusIndicator = useCallback(\n (index: number) => {\n const el = indicatorMapRef.current.get(index)\n\n el?.focus()\n carousel?.scrollTo(index)\n },\n [carousel],\n )\n\n const onKeyDown = useCallback(\n (index: number) => (ev: KeyboardEvent<HTMLButtonElement>) => {\n const lastIndex = snapCount - 1\n\n runKeyAction(ev, {\n ArrowDown: () => {\n if (horizontal) return\n\n index = index === lastIndex ? 0 : index + 1\n\n onFocusIndicator(index)\n },\n ArrowLeft: () => {\n if (!horizontal) return\n\n index = index === 0 ? lastIndex : index - 1\n\n onFocusIndicator(index)\n },\n ArrowRight: () => {\n if (!horizontal) return\n\n index = index === lastIndex ? 0 : index + 1\n\n onFocusIndicator(index)\n },\n ArrowUp: () => {\n if (horizontal) return\n\n index = index === 0 ? lastIndex : index - 1\n\n onFocusIndicator(index)\n },\n End: () => {\n onFocusIndicator(lastIndex)\n },\n Home: () => {\n onFocusIndicator(0)\n },\n })\n },\n [snapCount, horizontal, onFocusIndicator],\n )\n\n assignRef(controlRef, carousel)\n\n useEffect(() => {\n if (carousel) {\n carousel.on(\"reInit\", onInit)\n carousel.on(\"select\", onSelect)\n carousel.on(\"scroll\", onScroll)\n\n onScroll()\n\n return () => {\n carousel.off(\"reInit\", onInit)\n carousel.off(\"select\", onSelect)\n carousel.off(\"scroll\", onScroll)\n }\n }\n }, [carousel, onInit, onScroll, onSelect])\n\n useEffect(() => {\n const stop = hover && stopMouseEnterAutoplay\n const last = !carousel?.canScrollNext()\n\n if (carousel && autoplay && !stop && !last) {\n timeoutId.current = setInterval(() => {\n carousel.scrollNext()\n }, delay)\n } else {\n if (timeoutId.current) clearInterval(timeoutId.current)\n\n timeoutId.current = null\n }\n\n return () => {\n if (timeoutId.current) clearInterval(timeoutId.current)\n }\n }, [autoplay, carousel, delay, hover, stopMouseEnterAutoplay])\n\n useUpdateEffect(() => {\n if (!carousel) return\n\n if (indexProp === undefined) return\n\n carousel.scrollTo(indexProp)\n }, [indexProp])\n\n useUpdateEffect(() => {\n if (!carousel) return\n\n carousel.reInit()\n }, [\n carousel,\n total,\n align,\n axis,\n containScroll,\n dragFree,\n duration,\n inViewThreshold,\n loop,\n skipSnaps,\n slidesToScroll,\n ])\n\n const getRootProps: PropGetter<\"section\"> = useCallback(\n ({ ref, ...props } = {}) =>\n mergeProps(\n {\n id,\n \"aria-roledescription\": \"carousel\",\n \"data-orientation\": orientation,\n },\n rest,\n {\n ref,\n onMouseEnter,\n onMouseLeave,\n },\n props,\n )(),\n [id, onMouseEnter, onMouseLeave, rest, orientation],\n )\n\n const getListProps: PropGetter = useCallback(\n ({ ref, ...props } = {}) => ({\n id: listId,\n \"aria-live\": autoplay ? \"off\" : \"polite\",\n \"data-orientation\": orientation,\n ...props,\n ref: mergeRefs(ref, listRef, carouselRef),\n }),\n [autoplay, listId, orientation, carouselRef],\n )\n\n const getItemProps: RequiredPropGetter<\"div\", { index: number }> =\n useCallback(\n ({ index: indexProp, ...props }) => {\n const page = indexProp + 1\n const selected = index === indexProp\n\n return {\n id: `${listId}-${indexProp}`,\n \"aria-label\": t(\"{page} of {total}\", { page, total }),\n \"aria-roledescription\": \"slide\",\n \"data-index\": indexProp.toString(),\n \"data-orientation\": orientation,\n \"data-selected\": dataAttr(selected),\n role: \"tabpanel\",\n ...props,\n }\n },\n [index, listId, total, orientation, t],\n )\n\n const getPrevTriggerProps: PropGetter<\"button\"> = useCallback(\n (props = {}) => ({\n \"aria-controls\": listId,\n \"aria-label\": t(\"Go to previous slide\"),\n \"data-orientation\": orientation,\n disabled: !carousel?.canScrollPrev(),\n ...props,\n onClick: handlerAll(props.onClick, () => carousel?.scrollPrev()),\n }),\n [carousel, listId, orientation, t],\n )\n\n const getNextTriggerProps: PropGetter<\"button\"> = useCallback(\n (props = {}) => ({\n \"aria-controls\": listId,\n \"aria-label\": t(\"Go to next slide\"),\n \"data-orientation\": orientation,\n disabled: !carousel?.canScrollNext(),\n ...props,\n onClick: handlerAll(props.onClick, () => carousel?.scrollNext()),\n }),\n [carousel, listId, orientation, t],\n )\n\n const getIndicatorsProps: PropGetter = useCallback(\n (props = {}) => ({\n \"aria-label\": t(\"Slides\"),\n \"aria-orientation\": orientation,\n role: \"tablist\",\n ...props,\n }),\n [orientation, t],\n )\n\n const getIndicatorProps: RequiredPropGetter<\"button\", { index: number }> =\n useCallback(\n ({ ref, index: indexProp, ...props }) => {\n const page = indexProp + 1\n const selected = index === indexProp\n\n return {\n type: \"button\" as const,\n \"aria-controls\": `${listId}-${indexProp}`,\n \"aria-label\": t(\"Go to {page} slide\", { page }),\n \"aria-selected\": selected,\n \"data-index\": indexProp.toString(),\n \"data-orientation\": orientation,\n \"data-selected\": dataAttr(selected),\n role: \"tab\",\n tabIndex: selected ? 0 : -1,\n ...props,\n ref: mergeRefs(ref, (node) => {\n indicatorMapRef.current.set(indexProp, node)\n }),\n onClick: handlerAll(props.onClick, () =>\n carousel?.scrollTo(indexProp),\n ),\n onKeyDown: handlerAll(props.onKeyDown, onKeyDown(indexProp)),\n }\n },\n [index, listId, t, orientation, onKeyDown, carousel],\n )\n\n return {\n carousel,\n index,\n setIndex,\n snapCount,\n total,\n getIndicatorProps,\n getIndicatorsProps,\n getItemProps,\n getListProps,\n getNextTriggerProps,\n getPrevTriggerProps,\n getRootProps,\n }\n}\n\nexport type UseCarouselReturn = ReturnType<typeof useCarousel>\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA+CA,MAAM,CAAC,iBAAiB,sBAAsBA,8BAA+B,EAC3E,MAAM,mBACP,CAAC;AA4IF,MAAa,eAAe,EAC1B,IACA,QAAQ,UACR,WAAW,OACX,gBAAgB,OAChB,YACA,eAAe,GACf,QAAQ,KACR,WAAW,OACX,YAAY,MACZ,WAAW,IACX,OAAO,WACP,kBAAkB,GAClB,OAAO,MACP,cAAc,cACd,UAAU,EAAE,EACZ,YAAY,OACZ,iBAAiB,GACjB,yBAAyB,MACzB,YAAY,WACZ,aAAa,kBAAkB,MAC/B,cAAc,MACd,UACA,kBACA,GAAG,SACiB,EAAE,KAAK;CAC3B,MAAM,EAAE,MAAMC,8BAAQ,WAAW;CACjC,MAAM,CAAC,OAAO,YAAYC,gEAAqB;EAC7C,cAAc;EACd,OAAO;EACP;EACD,CAAC;CACF,MAAM,C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ACH,2DAAoB,MAAM,eAAe,UAAU,YAAY,CAAC;EACjE,GACD;EAAC;EAAU;EAAQ;EAAa;EAAE,CACnC;CAED,MAAMC,8CACH,QAAQ,EAAE,MAAM;EACf,iBAAiB;EACjB,cAAc,EAAE,mBAAmB;EACnC,oBAAoB;EACpB,UAAU,CAAC,UAAU,eAAe;EACpC,GAAG;EACH,2DAAoB,MAAM,eAAe,UAAU,YAAY,CAAC;EACjE,GACD;EAAC;EAAU;EAAQ;EAAa;EAAE,CACnC;CAED,MAAMC,6CACH,QAAQ,EAAE,MAAM;EACf,cAAc,EAAE,SAAS;EACzB,oBAAoB;EACpB,MAAM;EACN,GAAG;EACJ,GACD,CAAC,aAAa,EAAE,CACjB;AA+BD,QAAO;EACL;EACA;EACA;EACA;EACA;EACA,2CAjCG,EAAE,KAAK,OAAOH,aAAW,GAAG,YAAY;GACvC,MAAM,OAAOA,cAAY;GACzB,MAAM,WAAW,UAAUA;AAE3B,UAAO;IACL,MAAM;IACN,iBAAiB,GAAG,OAAO,GAAGA;IAC9B,cAAc,EAAE,sBAAsB,EAAE,MAAM,CAAC;IAC/C,iBAAiB;IACjB,cAAcA,YAAU,UAAU;IAClC,oBAAoB;IACpB,iEAA0B,SAAS;IACnC,MAAM;IACN,UAAU,WAAW,IAAI;IACzB,GAAG;IACH,KAAKF,sBAAU,MAAM,SAAS;AAC5B,qBAAgB,QAAQ,IAAIE,aAAW,KAAK;MAC5C;IACF,2DAAoB,MAAM,eACxB,UAAU,SAASA,YAAU,CAC9B;IACD,6DAAsB,MAAM,WAAW,UAAUA,YAAU,CAAC;IAC7D;KAEH;GAAC;GAAO;GAAQ;GAAG;GAAa;GAAW;GAAS,CACrD;EASD;EACA;EACA;EACA;EACA;EACA;EACD"}
|
|
@@ -6,6 +6,8 @@ const require_dom = require('../../utils/dom.cjs');
|
|
|
6
6
|
const require_effect = require('../../utils/effect.cjs');
|
|
7
7
|
const require_ref = require('../../utils/ref.cjs');
|
|
8
8
|
const require_utils_index = require('../../utils/index.cjs');
|
|
9
|
+
const require_props = require('../../core/components/props.cjs');
|
|
10
|
+
require('../../core/index.cjs');
|
|
9
11
|
const require_hooks_use_controllable_state_index = require('../../hooks/use-controllable-state/index.cjs');
|
|
10
12
|
const require_hooks_use_descendants_index = require('../../hooks/use-descendants/index.cjs');
|
|
11
13
|
let react = require("react");
|
|
@@ -63,12 +65,7 @@ const useTabs = ({ id, defaultIndex = 0, index: indexProp, manual = false, orien
|
|
|
63
65
|
setIndex(indexProp);
|
|
64
66
|
setFocusedIndex(indexProp);
|
|
65
67
|
}, [indexProp]);
|
|
66
|
-
const getRootProps = (0, react.useCallback)(({ ref, ...props } = {}) => ({
|
|
67
|
-
"data-orientation": orientation,
|
|
68
|
-
...rest,
|
|
69
|
-
...props,
|
|
70
|
-
ref: require_ref.mergeRefs(ref, rest.ref)
|
|
71
|
-
}), [orientation, rest]);
|
|
68
|
+
const getRootProps = (0, react.useCallback)(({ ref, ...props } = {}) => require_props.mergeProps({ "data-orientation": orientation }, rest, props, { ref })(), [orientation, rest]);
|
|
72
69
|
const getListProps = (0, react.useCallback)((props = {}) => ({
|
|
73
70
|
"aria-orientation": orientation,
|
|
74
71
|
role: "tablist",
|
|
@@ -116,7 +113,7 @@ const useTab = ({ id, disabled, index, ...rest }) => {
|
|
|
116
113
|
return {
|
|
117
114
|
index,
|
|
118
115
|
selected,
|
|
119
|
-
getRootProps: (0, react.useCallback)(({ ref, ...props } = {}) => ({
|
|
116
|
+
getRootProps: (0, react.useCallback)(({ ref, ...props } = {}) => require_props.mergeProps({
|
|
120
117
|
id,
|
|
121
118
|
type: "button",
|
|
122
119
|
"aria-controls": tabPanelId,
|
|
@@ -124,13 +121,11 @@ const useTab = ({ id, disabled, index, ...rest }) => {
|
|
|
124
121
|
"data-orientation": orientation,
|
|
125
122
|
disabled,
|
|
126
123
|
role: "tab",
|
|
127
|
-
tabIndex: selected ? 0 : -1
|
|
128
|
-
|
|
129
|
-
...props,
|
|
130
|
-
ref: require_ref.mergeRefs(ref, register),
|
|
124
|
+
tabIndex: selected ? 0 : -1
|
|
125
|
+
}, rest, props, { ref: require_ref.mergeRefs(ref, register) }, {
|
|
131
126
|
onClick: (0, require_utils_index.utils_exports.handlerAll)(props.onClick, rest.onClick, onClick),
|
|
132
127
|
onFocus: (0, require_utils_index.utils_exports.handlerAll)(props.onFocus, rest.onFocus, onFocus)
|
|
133
|
-
}), [
|
|
128
|
+
})({ mergeEvent: false }), [
|
|
134
129
|
disabled,
|
|
135
130
|
id,
|
|
136
131
|
onClick,
|
|
@@ -152,17 +147,14 @@ const useTabPanel = ({ id, "aria-labelledby": ariaLabelledbyProp, index, ...rest
|
|
|
152
147
|
return {
|
|
153
148
|
index,
|
|
154
149
|
selected,
|
|
155
|
-
getRootProps: (0, react.useCallback)(({ ref, "aria-labelledby": ariaLabelledby, ...props } = {}) => ({
|
|
150
|
+
getRootProps: (0, react.useCallback)(({ ref, "aria-labelledby": ariaLabelledby, ...props } = {}) => require_props.mergeProps({
|
|
156
151
|
id,
|
|
157
152
|
"aria-labelledby": (0, require_utils_index.utils_exports.cx)(ariaLabelledbyProp, ariaLabelledby, tabId),
|
|
158
153
|
"data-orientation": orientation,
|
|
159
154
|
hidden: !selected,
|
|
160
155
|
role: "tabpanel",
|
|
161
|
-
tabIndex: selected ? 0 : -1
|
|
162
|
-
|
|
163
|
-
...props,
|
|
164
|
-
ref: require_ref.mergeRefs(ref, register)
|
|
165
|
-
}), [
|
|
156
|
+
tabIndex: selected ? 0 : -1
|
|
157
|
+
}, rest, props, { ref: require_ref.mergeRefs(ref, register) })(), [
|
|
166
158
|
id,
|
|
167
159
|
ariaLabelledbyProp,
|
|
168
160
|
orientation,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-tabs.cjs","names":["createDescendants","createContext","useControllableState","getRootProps: PropGetter","mergeRefs","getListProps: PropGetter"],"sources":["../../../../src/components/tabs/use-tabs.ts"],"sourcesContent":["\"use client\"\n\nimport type { KeyboardEvent } from \"react\"\nimport type { HTMLProps, Orientation, PropGetter } from \"../../core\"\nimport { useCallback, useId, useState } from \"react\"\nimport { useControllableState } from \"../../hooks/use-controllable-state\"\nimport { createDescendants } from \"../../hooks/use-descendants\"\nimport {\n createContext,\n cx,\n handlerAll,\n isUndefined,\n mergeRefs,\n runKeyAction,\n useUpdateEffect,\n} from \"../../utils\"\n\nconst {\n DescendantsContext: TabDescendantsContext,\n useDescendant: useTabDescendant,\n useDescendants: useTabDescendants,\n} = createDescendants<HTMLButtonElement>()\n\nexport { TabDescendantsContext, useTabDescendant, useTabDescendants }\n\nconst {\n DescendantsContext: TabPanelDescendantsContext,\n useDescendant: useTabPanelDescendant,\n useDescendants: useTabPanelDescendants,\n} = createDescendants<HTMLDivElement>()\n\nexport {\n TabPanelDescendantsContext,\n useTabPanelDescendant,\n useTabPanelDescendants,\n}\n\ninterface TabsContext extends Omit<\n UseTabsReturn,\n \"getRootProps\" | \"tabDescendants\" | \"tabPanelDescendants\"\n> {}\n\nconst [TabsContext, useTabsContext] = createContext<TabsContext>({\n name: \"TabsContext\",\n})\n\nexport { TabsContext, useTabsContext }\n\nexport interface UseTabsProps extends Omit<HTMLProps, \"onChange\"> {\n /**\n * The index of the selected tab.\n */\n defaultIndex?: number\n /**\n * The index of the selected tab.\n */\n index?: number\n /**\n * If `true`, the tabs will be manually activated and display its panel by pressing Space or Enter.\n *\n * If `false`, the tabs will be automatically activated and their panel is displayed when they receive focus.\n *\n * @default false\n */\n manual?: boolean\n /**\n * The orientation of the tabs.\n *\n * @default 'horizontal'\n */\n orientation?: Orientation\n /**\n * The callback invoked when the index changes.\n */\n onChange?: (index: number) => void\n}\n\nexport const useTabs = ({\n id,\n defaultIndex = 0,\n index: indexProp,\n manual = false,\n orientation = \"horizontal\",\n onChange,\n ...rest\n}: UseTabsProps = {}) => {\n const uuid = useId()\n const tabDescendants = useTabDescendants()\n const tabPanelDescendants = useTabPanelDescendants()\n const [index, setIndex] = useControllableState({\n defaultValue: defaultIndex,\n value: indexProp,\n onChange,\n })\n const [focusedIndex, setFocusedIndex] = useState<number>(index)\n const horizontal = orientation === \"horizontal\"\n\n id ??= uuid\n\n const onFocusFirstTab = useCallback(() => {\n const first = tabDescendants.enabledFirstValue()\n\n if (first) first.node.focus()\n }, [tabDescendants])\n\n const onFocusLastTab = useCallback(() => {\n const last = tabDescendants.enabledLastValue()\n\n if (last) last.node.focus()\n }, [tabDescendants])\n\n const onFocusNextTab = useCallback(() => {\n const next = tabDescendants.enabledNextValue(focusedIndex)\n\n if (next) next.node.focus()\n }, [tabDescendants, focusedIndex])\n\n const onFocusPrevTab = useCallback(() => {\n const prev = tabDescendants.enabledPrevValue(focusedIndex)\n\n if (prev) prev.node.focus()\n }, [tabDescendants, focusedIndex])\n\n const onKeyDown = useCallback(\n (ev: KeyboardEvent<HTMLDivElement>) => {\n runKeyAction(ev, {\n ArrowDown: !horizontal ? onFocusNextTab : undefined,\n ArrowLeft: horizontal ? onFocusPrevTab : undefined,\n ArrowRight: horizontal ? onFocusNextTab : undefined,\n ArrowUp: !horizontal ? onFocusPrevTab : undefined,\n End: onFocusLastTab,\n Home: onFocusFirstTab,\n })\n },\n [\n horizontal,\n onFocusNextTab,\n onFocusPrevTab,\n onFocusLastTab,\n onFocusFirstTab,\n ],\n )\n\n useUpdateEffect(() => {\n if (isUndefined(indexProp)) return\n\n setIndex(indexProp)\n setFocusedIndex(indexProp)\n }, [indexProp])\n\n const getRootProps: PropGetter = useCallback(\n ({ ref, ...props } = {}) => ({\n \"data-orientation\": orientation,\n ...rest,\n ...props,\n ref: mergeRefs(ref, rest.ref),\n }),\n [orientation, rest],\n )\n\n const getListProps: PropGetter = useCallback(\n (props = {}) => ({\n \"aria-orientation\": orientation,\n role: \"tablist\",\n ...props,\n onKeyDown: handlerAll(props.onKeyDown, onKeyDown),\n }),\n [orientation, onKeyDown],\n )\n\n return {\n id,\n focusedIndex,\n index,\n manual,\n orientation,\n setFocusedIndex,\n setIndex,\n tabDescendants,\n tabPanelDescendants,\n getListProps,\n getRootProps,\n }\n}\n\nexport type UseTabsReturn = ReturnType<typeof useTabs>\n\nexport interface UseTabProps extends HTMLProps<\"button\"> {\n /**\n * The index of the tab.\n */\n index: number\n}\n\nexport const useTab = ({ id, disabled, index, ...rest }: UseTabProps) => {\n const {\n id: rootId,\n index: selectedIndex,\n manual,\n orientation,\n setFocusedIndex,\n setIndex,\n } = useTabsContext()\n const { register } = useTabDescendant({ disabled })\n const tabPanelId = `${rootId}-panel-${index}`\n const selected = index === selectedIndex\n\n id ??= `${rootId}-tab-${index}`\n\n const onClick = useCallback(() => {\n if (!disabled) setIndex(index)\n }, [index, setIndex, disabled])\n\n const onFocus = useCallback(() => {\n if (disabled) return\n\n setFocusedIndex(index)\n\n if (!manual) setIndex(index)\n }, [setFocusedIndex, index, manual, disabled, setIndex])\n\n const getRootProps: PropGetter<\"button\"> = useCallback(\n ({ ref, ...props } = {}) => ({\n id,\n type: \"button\",\n \"aria-controls\": tabPanelId,\n \"aria-selected\": selected,\n \"data-orientation\": orientation,\n disabled,\n role: \"tab\",\n tabIndex: selected ? 0 : -1,\n ...rest,\n ...props,\n ref: mergeRefs(ref, register),\n onClick: handlerAll(props.onClick, rest.onClick, onClick),\n onFocus: handlerAll(props.onFocus, rest.onFocus, onFocus),\n }),\n [\n disabled,\n id,\n onClick,\n onFocus,\n orientation,\n register,\n rest,\n selected,\n tabPanelId,\n ],\n )\n\n return { index, selected, getRootProps }\n}\n\nexport type UseTabReturn = ReturnType<typeof useTab>\n\nexport interface UseTabPanelProps extends HTMLProps {\n /**\n * The index of the tab panel.\n */\n index: number\n}\n\nexport const useTabPanel = ({\n id,\n \"aria-labelledby\": ariaLabelledbyProp,\n index,\n ...rest\n}: UseTabPanelProps) => {\n const { id: rootId, index: selectedIndex, orientation } = useTabsContext()\n const { register } = useTabPanelDescendant()\n const tabId = `${rootId}-tab-${index}`\n const selected = index === selectedIndex\n\n id ??= `${rootId}-panel-${index}`\n\n const getRootProps: PropGetter = useCallback(\n ({ ref, \"aria-labelledby\": ariaLabelledby, ...props } = {}) => ({\n id,\n \"aria-labelledby\": cx(ariaLabelledbyProp, ariaLabelledby, tabId),\n \"data-orientation\": orientation,\n hidden: !selected,\n role: \"tabpanel\",\n tabIndex: selected ? 0 : -1,\n ...rest,\n ...props,\n ref: mergeRefs(ref, register),\n }),\n [id, ariaLabelledbyProp, orientation, register, rest, selected, tabId],\n )\n\n return { index, selected, getRootProps }\n}\n\nexport type UseTabPanelReturn = ReturnType<typeof useTabPanel>\n"],"mappings":";;;;;;;;;;;;;AAiBA,MAAM,EACJ,oBAAoB,uBACpB,eAAe,kBACf,gBAAgB,sBACdA,uDAAsC;AAI1C,MAAM,EACJ,oBAAoB,4BACpB,eAAe,uBACf,gBAAgB,2BACdA,uDAAmC;AAavC,MAAM,CAAC,aAAa,kBAAkBC,8BAA2B,EAC/D,MAAM,eACP,CAAC;AAiCF,MAAa,WAAW,EACtB,IACA,eAAe,GACf,OAAO,WACP,SAAS,OACT,cAAc,cACd,UACA,GAAG,SACa,EAAE,KAAK;CACvB,MAAM,yBAAc;CACpB,MAAM,iBAAiB,mBAAmB;CAC1C,MAAM,sBAAsB,wBAAwB;CACpD,MAAM,CAAC,OAAO,YAAYC,gEAAqB;EAC7C,cAAc;EACd,OAAO;EACP;EACD,CAAC;CACF,MAAM,CAAC,cAAc,uCAAoC,MAAM;CAC/D,MAAM,aAAa,gBAAgB;AAEnC,QAAO;CAEP,MAAM,+CAAoC;EACxC,MAAM,QAAQ,eAAe,mBAAmB;AAEhD,MAAI,MAAO,OAAM,KAAK,OAAO;IAC5B,CAAC,eAAe,CAAC;CAEpB,MAAM,8CAAmC;EACvC,MAAM,OAAO,eAAe,kBAAkB;AAE9C,MAAI,KAAM,MAAK,KAAK,OAAO;IAC1B,CAAC,eAAe,CAAC;CAEpB,MAAM,8CAAmC;EACvC,MAAM,OAAO,eAAe,iBAAiB,aAAa;AAE1D,MAAI,KAAM,MAAK,KAAK,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AAU;EAAS,CAAC;CAE/B,MAAM,uCAA4B;AAChC,MAAI,SAAU;AAEd,kBAAgB,MAAM;AAEtB,MAAI,CAAC,OAAQ,UAAS,MAAM;IAC3B;EAAC;EAAiB;EAAO;EAAQ;EAAU;EAAS,CAAC;AA+BxD,QAAO;EAAE;EAAO;EAAU,sCA5BvB,EAAE,KAAK,GAAG,UAAU,EAAE,MAAM;GAC3B;GACA,MAAM;GACN,iBAAiB;GACjB,iBAAiB;GACjB,oBAAoB;GACpB;GACA,MAAM;GACN,UAAU,WAAW,IAAI;GACzB,GAAG;GACH,GAAG;GACH,KAAKD,sBAAU,KAAK,SAAS;GAC7B,2DAAoB,MAAM,SAAS,KAAK,SAAS,QAAQ;GACzD,2DAAoB,MAAM,SAAS,KAAK,SAAS,QAAQ;GAC1D,GACD;GACE;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACD,CACF;EAEuC;;AAY1C,MAAa,eAAe,EAC1B,IACA,mBAAmB,oBACnB,OACA,GAAG,WACmB;CACtB,MAAM,EAAE,IAAI,QAAQ,OAAO,eAAe,gBAAgB,gBAAgB;CAC1E,MAAM,EAAE,aAAa,uBAAuB;CAC5C,MAAM,QAAQ,GAAG,OAAO,OAAO;CAC/B,MAAM,WAAW,UAAU;AAE3B,QAAO,GAAG,OAAO,SAAS;AAiB1B,QAAO;EAAE;EAAO;EAAU,sCAdvB,EAAE,KAAK,mBAAmB,gBAAgB,GAAG,UAAU,EAAE,MAAM;GAC9D;GACA,6DAAsB,oBAAoB,gBAAgB,MAAM;GAChE,oBAAoB;GACpB,QAAQ,CAAC;GACT,MAAM;GACN,UAAU,WAAW,IAAI;GACzB,GAAG;GACH,GAAG;GACH,KAAKA,sBAAU,KAAK,SAAS;GAC9B,GACD;GAAC;GAAI;GAAoB;GAAa;GAAU;GAAM;GAAU;GAAM,CACvE;EAEuC"}
|
|
1
|
+
{"version":3,"file":"use-tabs.cjs","names":["createDescendants","createContext","useControllableState","getRootProps: PropGetter","mergeProps","getListProps: PropGetter","mergeRefs"],"sources":["../../../../src/components/tabs/use-tabs.ts"],"sourcesContent":["\"use client\"\n\nimport type { KeyboardEvent } from \"react\"\nimport type { HTMLProps, Orientation, PropGetter } from \"../../core\"\nimport { useCallback, useId, useState } from \"react\"\nimport { mergeProps } from \"../../core\"\nimport { useControllableState } from \"../../hooks/use-controllable-state\"\nimport { createDescendants } from \"../../hooks/use-descendants\"\nimport {\n createContext,\n cx,\n handlerAll,\n isUndefined,\n mergeRefs,\n runKeyAction,\n useUpdateEffect,\n} from \"../../utils\"\n\nconst {\n DescendantsContext: TabDescendantsContext,\n useDescendant: useTabDescendant,\n useDescendants: useTabDescendants,\n} = createDescendants<HTMLButtonElement>()\n\nexport { TabDescendantsContext, useTabDescendant, useTabDescendants }\n\nconst {\n DescendantsContext: TabPanelDescendantsContext,\n useDescendant: useTabPanelDescendant,\n useDescendants: useTabPanelDescendants,\n} = createDescendants<HTMLDivElement>()\n\nexport {\n TabPanelDescendantsContext,\n useTabPanelDescendant,\n useTabPanelDescendants,\n}\n\ninterface TabsContext extends Omit<\n UseTabsReturn,\n \"getRootProps\" | \"tabDescendants\" | \"tabPanelDescendants\"\n> {}\n\nconst [TabsContext, useTabsContext] = createContext<TabsContext>({\n name: \"TabsContext\",\n})\n\nexport { TabsContext, useTabsContext }\n\nexport interface UseTabsProps extends Omit<HTMLProps, \"onChange\"> {\n /**\n * The index of the selected tab.\n */\n defaultIndex?: number\n /**\n * The index of the selected tab.\n */\n index?: number\n /**\n * If `true`, the tabs will be manually activated and display its panel by pressing Space or Enter.\n *\n * If `false`, the tabs will be automatically activated and their panel is displayed when they receive focus.\n *\n * @default false\n */\n manual?: boolean\n /**\n * The orientation of the tabs.\n *\n * @default 'horizontal'\n */\n orientation?: Orientation\n /**\n * The callback invoked when the index changes.\n */\n onChange?: (index: number) => void\n}\n\nexport const useTabs = ({\n id,\n defaultIndex = 0,\n index: indexProp,\n manual = false,\n orientation = \"horizontal\",\n onChange,\n ...rest\n}: UseTabsProps = {}) => {\n const uuid = useId()\n const tabDescendants = useTabDescendants()\n const tabPanelDescendants = useTabPanelDescendants()\n const [index, setIndex] = useControllableState({\n defaultValue: defaultIndex,\n value: indexProp,\n onChange,\n })\n const [focusedIndex, setFocusedIndex] = useState<number>(index)\n const horizontal = orientation === \"horizontal\"\n\n id ??= uuid\n\n const onFocusFirstTab = useCallback(() => {\n const first = tabDescendants.enabledFirstValue()\n\n if (first) first.node.focus()\n }, [tabDescendants])\n\n const onFocusLastTab = useCallback(() => {\n const last = tabDescendants.enabledLastValue()\n\n if (last) last.node.focus()\n }, [tabDescendants])\n\n const onFocusNextTab = useCallback(() => {\n const next = tabDescendants.enabledNextValue(focusedIndex)\n\n if (next) next.node.focus()\n }, [tabDescendants, focusedIndex])\n\n const onFocusPrevTab = useCallback(() => {\n const prev = tabDescendants.enabledPrevValue(focusedIndex)\n\n if (prev) prev.node.focus()\n }, [tabDescendants, focusedIndex])\n\n const onKeyDown = useCallback(\n (ev: KeyboardEvent<HTMLDivElement>) => {\n runKeyAction(ev, {\n ArrowDown: !horizontal ? onFocusNextTab : undefined,\n ArrowLeft: horizontal ? onFocusPrevTab : undefined,\n ArrowRight: horizontal ? onFocusNextTab : undefined,\n ArrowUp: !horizontal ? onFocusPrevTab : undefined,\n End: onFocusLastTab,\n Home: onFocusFirstTab,\n })\n },\n [\n horizontal,\n onFocusNextTab,\n onFocusPrevTab,\n onFocusLastTab,\n onFocusFirstTab,\n ],\n )\n\n useUpdateEffect(() => {\n if (isUndefined(indexProp)) return\n\n setIndex(indexProp)\n setFocusedIndex(indexProp)\n }, [indexProp])\n\n const getRootProps: PropGetter = useCallback(\n ({ ref, ...props } = {}) =>\n mergeProps({ \"data-orientation\": orientation }, rest, props, { ref })(),\n [orientation, rest],\n )\n\n const getListProps: PropGetter = useCallback(\n (props = {}) => ({\n \"aria-orientation\": orientation,\n role: \"tablist\",\n ...props,\n onKeyDown: handlerAll(props.onKeyDown, onKeyDown),\n }),\n [orientation, onKeyDown],\n )\n\n return {\n id,\n focusedIndex,\n index,\n manual,\n orientation,\n setFocusedIndex,\n setIndex,\n tabDescendants,\n tabPanelDescendants,\n getListProps,\n getRootProps,\n }\n}\n\nexport type UseTabsReturn = ReturnType<typeof useTabs>\n\nexport interface UseTabProps extends HTMLProps<\"button\"> {\n /**\n * The index of the tab.\n */\n index: number\n}\n\nexport const useTab = ({ id, disabled, index, ...rest }: UseTabProps) => {\n const {\n id: rootId,\n index: selectedIndex,\n manual,\n orientation,\n setFocusedIndex,\n setIndex,\n } = useTabsContext()\n const { register } = useTabDescendant({ disabled })\n const tabPanelId = `${rootId}-panel-${index}`\n const selected = index === selectedIndex\n\n id ??= `${rootId}-tab-${index}`\n\n const onClick = useCallback(() => {\n if (!disabled) setIndex(index)\n }, [index, setIndex, disabled])\n\n const onFocus = useCallback(() => {\n if (disabled) return\n\n setFocusedIndex(index)\n\n if (!manual) setIndex(index)\n }, [setFocusedIndex, index, manual, disabled, setIndex])\n\n const getRootProps: PropGetter<\"button\"> = useCallback(\n ({ ref, ...props } = {}) =>\n mergeProps(\n {\n id,\n type: \"button\",\n \"aria-controls\": tabPanelId,\n \"aria-selected\": selected,\n \"data-orientation\": orientation,\n disabled,\n role: \"tab\",\n tabIndex: selected ? 0 : -1,\n },\n rest,\n props,\n { ref: mergeRefs(ref, register) },\n {\n onClick: handlerAll(props.onClick, rest.onClick, onClick),\n onFocus: handlerAll(props.onFocus, rest.onFocus, onFocus),\n },\n )({ mergeEvent: false }),\n [\n disabled,\n id,\n onClick,\n onFocus,\n orientation,\n register,\n rest,\n selected,\n tabPanelId,\n ],\n )\n\n return { index, selected, getRootProps }\n}\n\nexport type UseTabReturn = ReturnType<typeof useTab>\n\nexport interface UseTabPanelProps extends HTMLProps {\n /**\n * The index of the tab panel.\n */\n index: number\n}\n\nexport const useTabPanel = ({\n id,\n \"aria-labelledby\": ariaLabelledbyProp,\n index,\n ...rest\n}: UseTabPanelProps) => {\n const { id: rootId, index: selectedIndex, orientation } = useTabsContext()\n const { register } = useTabPanelDescendant()\n const tabId = `${rootId}-tab-${index}`\n const selected = index === selectedIndex\n\n id ??= `${rootId}-panel-${index}`\n\n const getRootProps: PropGetter = useCallback(\n ({ ref, \"aria-labelledby\": ariaLabelledby, ...props } = {}) =>\n mergeProps(\n {\n id,\n \"aria-labelledby\": cx(ariaLabelledbyProp, ariaLabelledby, tabId),\n \"data-orientation\": orientation,\n hidden: !selected,\n role: \"tabpanel\",\n tabIndex: selected ? 0 : -1,\n },\n rest,\n props,\n { ref: mergeRefs(ref, register) },\n )(),\n [id, ariaLabelledbyProp, orientation, register, rest, selected, tabId],\n )\n\n return { index, selected, getRootProps }\n}\n\nexport type UseTabPanelReturn = ReturnType<typeof useTabPanel>\n"],"mappings":";;;;;;;;;;;;;;;AAkBA,MAAM,EACJ,oBAAoB,uBACpB,eAAe,kBACf,gBAAgB,sBACdA,uDAAsC;AAI1C,MAAM,EACJ,oBAAoB,4BACpB,eAAe,uBACf,gBAAgB,2BACdA,uDAAmC;AAavC,MAAM,CAAC,aAAa,kBAAkBC,8BAA2B,EAC/D,MAAM,eACP,CAAC;AAiCF,MAAa,WAAW,EACtB,IACA,eAAe,GACf,OAAO,WACP,SAAS,OACT,cAAc,cACd,UACA,GAAG,SACa,EAAE,KAAK;CACvB,MAAM,yBAAc;CACpB,MAAM,iBAAiB,mBAAmB;CAC1C,MAAM,sBAAsB,wBAAwB;CACpD,MAAM,CAAC,OAAO,YAAYC,gEAAqB;EAC7C,cAAc;EACd,OAAO;EACP;EACD,CAAC;CACF,MAAM,CAAC,cAAc,uCAAoC,MAAM;CAC/D,MAAM,aAAa,gBAAgB;AAEnC,QAAO;CAEP,MAAM,+CAAoC;EACxC,MAAM,QAAQ,eAAe,mBAAmB;AAEhD,MAAI,MAAO,OAAM,KAAK,OAAO;IAC5B,CAAC,eAAe,CAAC;CAEpB,MAAM,8CAAmC;EACvC,MAAM,OAAO,eAAe,kBAAkB;AAE9C,MAAI,KAA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E,KAAKE,sBAAU,KAAK,SAAS,EAAE,CAClC,EAAE,EACL;GAAC;GAAI;GAAoB;GAAa;GAAU;GAAM;GAAU;GAAM,CACvE;EAEuC"}
|
|
@@ -5,6 +5,8 @@ import { runKeyAction, useIds } from "../../utils/dom.js";
|
|
|
5
5
|
import { useUpdateEffect } from "../../utils/effect.js";
|
|
6
6
|
import { assignRef, mergeRefs } from "../../utils/ref.js";
|
|
7
7
|
import { utils_exports } from "../../utils/index.js";
|
|
8
|
+
import { mergeProps } from "../../core/components/props.js";
|
|
9
|
+
import "../../core/index.js";
|
|
8
10
|
import { useControllableState } from "../../hooks/use-controllable-state/index.js";
|
|
9
11
|
import { useI18n } from "../../providers/i18n-provider/i18n-provider.js";
|
|
10
12
|
import "../../providers/i18n-provider/index.js";
|
|
@@ -169,16 +171,15 @@ const useCarousel = ({ id, align = "center", autoplay = false, containScroll = f
|
|
|
169
171
|
skipSnaps,
|
|
170
172
|
slidesToScroll
|
|
171
173
|
]);
|
|
172
|
-
const getRootProps = useCallback(({ ref, ...props } = {}) => ({
|
|
174
|
+
const getRootProps = useCallback(({ ref, ...props } = {}) => mergeProps({
|
|
173
175
|
id,
|
|
174
176
|
"aria-roledescription": "carousel",
|
|
175
|
-
"data-orientation": orientation
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
}), [
|
|
177
|
+
"data-orientation": orientation
|
|
178
|
+
}, rest, {
|
|
179
|
+
ref,
|
|
180
|
+
onMouseEnter,
|
|
181
|
+
onMouseLeave
|
|
182
|
+
}, props)(), [
|
|
182
183
|
id,
|
|
183
184
|
onMouseEnter,
|
|
184
185
|
onMouseLeave,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-carousel.js","names":["createContext","snapCount","total","index","getRootProps: PropGetter<\"section\">","getListProps: PropGetter","getItemProps: RequiredPropGetter<\"div\", { index: number }>","indexProp","getPrevTriggerProps: PropGetter<\"button\">","getNextTriggerProps: PropGetter<\"button\">","getIndicatorsProps: PropGetter"],"sources":["../../../../src/components/carousel/use-carousel.ts"],"sourcesContent":["\"use client\"\n\nimport type {\n EmblaCarouselType,\n EmblaOptionsType,\n EmblaPluginType,\n} from \"embla-carousel\"\nimport type { KeyboardEvent, RefObject } from \"react\"\nimport type {\n HTMLProps,\n Orientation,\n PropGetter,\n RequiredPropGetter,\n} from \"../../core\"\nimport useEmblaCarousel from \"embla-carousel-react\"\nimport { useCallback, useEffect, useRef, useState } from \"react\"\nimport { useBoolean } from \"../../hooks/use-boolean\"\nimport { useControllableState } from \"../../hooks/use-controllable-state\"\nimport { useI18n } from \"../../providers/i18n-provider\"\nimport {\n assignRef,\n createContext,\n dataAttr,\n handlerAll,\n isFunction,\n mergeRefs,\n runKeyAction,\n useIds,\n useUpdateEffect,\n} from \"../../utils\"\n\ntype EmblaOptions = Required<EmblaOptionsType>\nexport type CarouselAlign = EmblaOptions[\"align\"]\nexport type CarouselContainScroll = EmblaOptions[\"containScroll\"]\nexport type CarouselInViewThreshold = EmblaOptions[\"inViewThreshold\"]\nexport type CarouselWatchDrag = EmblaOptions[\"watchDrag\"]\nexport type CarouselWatchResize = EmblaOptions[\"watchResize\"]\nexport type CarouselWatchSlides = EmblaOptions[\"watchSlides\"]\nexport type CarouselControl = EmblaCarouselType\nexport type CarouselPlugin = EmblaPluginType\n\nexport interface CarouselContext extends Omit<\n UseCarouselReturn,\n \"getRootProps\"\n> {}\n\nconst [CarouselContext, useCarouselContext] = createContext<CarouselContext>({\n name: \"CarouselContext\",\n})\n\nexport { CarouselContext, useCarouselContext }\n\nexport interface UseCarouselProps extends Omit<\n HTMLProps<\"section\">,\n \"onChange\"\n> {\n /**\n * The alignment of the carousel.\n *\n * @default 'center'\n */\n align?: CarouselAlign\n /**\n * If `true`, the carousel will be autoplay.\n *\n * @default false\n */\n autoplay?: boolean\n /**\n * Clear leading and trailing empty space that causes excessive scrolling.\n * Use trimSnaps to only use snap points that trigger scrolling or keepSnaps to keep them.\n *\n * @default false\n */\n containScroll?: CarouselContainScroll\n /**\n * Ref of the resizable item callback.\n */\n controlRef?: RefObject<CarouselControl | null>\n /**\n * The initial index of the carousel slide.\n *\n * @default 0\n */\n defaultIndex?: number\n /**\n * The number for the autoplay interval of the carousel.\n *\n * @default 4000\n */\n delay?: number\n /**\n * If `true`, momentum scrolling will be enabled.\n *\n * @default false\n */\n dragFree?: boolean\n /**\n * If `true`, carousel can be scrolled with mouse and touch interactions.\n *\n * @default true\n */\n draggable?: boolean\n /**\n * Set scroll duration when triggered by any of the API methods.\n * Higher numbers enables slower scrolling.\n * Drag interactions are not affected because duration is then determined by the drag force.\n *\n * @default 25\n */\n duration?: number\n /**\n * The index of the carousel slide.\n */\n index?: number\n /**\n * Choose a fraction representing the percentage portion of a slide that needs to be visible in order to be considered in view.\n *\n * @default 0\n */\n inViewThreshold?: CarouselInViewThreshold\n /**\n * If `true`, infinite looping.\n * Automatically falls back to false if slide content isn't enough to loop.\n *\n * @default true\n */\n loop?: boolean\n /**\n * The orientation of the carousel.\n *\n * @default 'horizontal'\n */\n orientation?: Orientation\n /**\n * Embla plugins to use.\n */\n plugins?: CarouselPlugin[]\n /**\n * If `true`, allow the carousel to skip scroll snaps if it's dragged vigorously.\n * Note that this option will be ignored if the dragFree option is set to true.\n *\n * @default false\n */\n skipSnaps?: boolean\n /**\n * The number of slides that should be scrolled with next or previous buttons.\n *\n * @default 1\n */\n slidesToScroll?: number\n /**\n * If `true`, autoplay will pause when the mouse entries over.\n *\n * @default true\n */\n stopMouseEnterAutoplay?: boolean\n /**\n * Enables for scrolling the carousel with mouse and touch interactions.\n * Set this to `false` to disable drag events or pass a custom callback to add your own drag logic.\n *\n * @default true\n */\n watchDrag?: CarouselWatchDrag\n /**\n * Embla automatically watches the container and slides for size changes and runs `reInit` when any size has changed.\n * Set this to `false` to disable this behaviour or pass a custom callback to add your own resize logic.\n *\n * @default true\n */\n watchResize?: CarouselWatchResize\n /**\n * Embla automatically watches the container for added and/or removed slides and runs `reInit` if needed.\n * Set this to `false` to disable this behaviour or pass a custom callback to add your own slides changed logic.\n *\n * @default true\n */\n watchSlides?: CarouselWatchSlides\n /**\n * The callback invoked when carousel slide selected.\n */\n onChange?: (index: number) => void\n /**\n * A callback that return the current scroll amount when the carousel is scrolled.\n */\n onScrollProgress?: (progress: number) => void\n}\n\nexport const useCarousel = ({\n id,\n align = \"center\",\n autoplay = false,\n containScroll = false,\n controlRef,\n defaultIndex = 0,\n delay = 4000,\n dragFree = false,\n draggable = true,\n duration = 25,\n index: indexProp,\n inViewThreshold = 0,\n loop = true,\n orientation = \"horizontal\",\n plugins = [],\n skipSnaps = false,\n slidesToScroll = 1,\n stopMouseEnterAutoplay = true,\n watchDrag = draggable,\n watchResize: watchResizeProp = true,\n watchSlides = true,\n onChange,\n onScrollProgress,\n ...rest\n}: UseCarouselProps = {}) => {\n const { t } = useI18n(\"carousel\")\n const [index, setIndex] = useControllableState({\n defaultValue: defaultIndex,\n value: indexProp,\n onChange,\n })\n const [rootId, listId] = useIds()\n const [hover, { off: onMouseLeave, on: onMouseEnter }] = useBoolean()\n const timeoutId = useRef<NodeJS.Timeout | null>(null)\n const indicatorMapRef = useRef<Map<number, HTMLButtonElement | null>>(\n new Map(),\n )\n const listRef = useRef<HTMLDivElement>(null)\n const horizontal = orientation === \"horizontal\"\n const axis = horizontal ? \"x\" : \"y\"\n const [snapCount, setSnapCount] = useState(0)\n const [total, setTotal] = useState(0)\n const watchResize = useCallback<Extract<CarouselWatchResize, Function>>(\n (methods, entries) => {\n const result = isFunction(watchResizeProp)\n ? watchResizeProp(methods, entries)\n : true\n const snapCount = methods.scrollSnapList().length\n const total = methods.slideNodes().length\n\n setSnapCount(snapCount)\n setTotal(total)\n\n return result\n },\n [watchResizeProp],\n )\n const [carouselRef, carousel] = useEmblaCarousel(\n {\n align,\n axis,\n container: listRef.current,\n containScroll,\n dragFree,\n duration,\n inViewThreshold,\n loop,\n skipSnaps,\n slidesToScroll,\n startIndex: defaultIndex,\n watchDrag,\n watchResize,\n watchSlides,\n },\n plugins,\n )\n\n id ??= rootId\n\n const onInit = useCallback((methods: CarouselControl) => {\n const snapCount = methods.scrollSnapList().length\n const total = methods.slideNodes().length\n\n setSnapCount(snapCount)\n setTotal(total)\n }, [])\n\n const onScroll = useCallback(() => {\n if (!carousel) return\n\n const progress = Math.round(\n Math.max(0, Math.min(1, carousel.scrollProgress())) * 100,\n )\n\n onScrollProgress?.(progress)\n }, [carousel, onScrollProgress])\n\n const onSelect = useCallback(() => {\n if (!carousel) return\n\n const index = carousel.selectedScrollSnap()\n\n setIndex(index)\n }, [carousel, setIndex])\n\n const onFocusIndicator = useCallback(\n (index: number) => {\n const el = indicatorMapRef.current.get(index)\n\n el?.focus()\n carousel?.scrollTo(index)\n },\n [carousel],\n )\n\n const onKeyDown = useCallback(\n (index: number) => (ev: KeyboardEvent<HTMLButtonElement>) => {\n const lastIndex = snapCount - 1\n\n runKeyAction(ev, {\n ArrowDown: () => {\n if (horizontal) return\n\n index = index === lastIndex ? 0 : index + 1\n\n onFocusIndicator(index)\n },\n ArrowLeft: () => {\n if (!horizontal) return\n\n index = index === 0 ? lastIndex : index - 1\n\n onFocusIndicator(index)\n },\n ArrowRight: () => {\n if (!horizontal) return\n\n index = index === lastIndex ? 0 : index + 1\n\n onFocusIndicator(index)\n },\n ArrowUp: () => {\n if (horizontal) return\n\n index = index === 0 ? lastIndex : index - 1\n\n onFocusIndicator(index)\n },\n End: () => {\n onFocusIndicator(lastIndex)\n },\n Home: () => {\n onFocusIndicator(0)\n },\n })\n },\n [snapCount, horizontal, onFocusIndicator],\n )\n\n assignRef(controlRef, carousel)\n\n useEffect(() => {\n if (carousel) {\n carousel.on(\"reInit\", onInit)\n carousel.on(\"select\", onSelect)\n carousel.on(\"scroll\", onScroll)\n\n onScroll()\n\n return () => {\n carousel.off(\"reInit\", onInit)\n carousel.off(\"select\", onSelect)\n carousel.off(\"scroll\", onScroll)\n }\n }\n }, [carousel, onInit, onScroll, onSelect])\n\n useEffect(() => {\n const stop = hover && stopMouseEnterAutoplay\n const last = !carousel?.canScrollNext()\n\n if (carousel && autoplay && !stop && !last) {\n timeoutId.current = setInterval(() => {\n carousel.scrollNext()\n }, delay)\n } else {\n if (timeoutId.current) clearInterval(timeoutId.current)\n\n timeoutId.current = null\n }\n\n return () => {\n if (timeoutId.current) clearInterval(timeoutId.current)\n }\n }, [autoplay, carousel, delay, hover, stopMouseEnterAutoplay])\n\n useUpdateEffect(() => {\n if (!carousel) return\n\n if (indexProp === undefined) return\n\n carousel.scrollTo(indexProp)\n }, [indexProp])\n\n useUpdateEffect(() => {\n if (!carousel) return\n\n carousel.reInit()\n }, [\n carousel,\n total,\n align,\n axis,\n containScroll,\n dragFree,\n duration,\n inViewThreshold,\n loop,\n skipSnaps,\n slidesToScroll,\n ])\n\n const getRootProps: PropGetter<\"section\"> = useCallback(\n ({ ref, ...props } = {}) => ({\n id,\n \"aria-roledescription\": \"carousel\",\n \"data-orientation\": orientation,\n ...rest,\n ...props,\n ref: mergeRefs(ref, rest.ref),\n onMouseEnter: handlerAll(props.onMouseEnter, onMouseEnter),\n onMouseLeave: handlerAll(props.onMouseLeave, onMouseLeave),\n }),\n [id, onMouseEnter, onMouseLeave, rest, orientation],\n )\n\n const getListProps: PropGetter = useCallback(\n ({ ref, ...props } = {}) => ({\n id: listId,\n \"aria-live\": autoplay ? \"off\" : \"polite\",\n \"data-orientation\": orientation,\n ...props,\n ref: mergeRefs(ref, listRef, carouselRef),\n }),\n [autoplay, listId, orientation, carouselRef],\n )\n\n const getItemProps: RequiredPropGetter<\"div\", { index: number }> =\n useCallback(\n ({ index: indexProp, ...props }) => {\n const page = indexProp + 1\n const selected = index === indexProp\n\n return {\n id: `${listId}-${indexProp}`,\n \"aria-label\": t(\"{page} of {total}\", { page, total }),\n \"aria-roledescription\": \"slide\",\n \"data-index\": indexProp.toString(),\n \"data-orientation\": orientation,\n \"data-selected\": dataAttr(selected),\n role: \"tabpanel\",\n ...props,\n }\n },\n [index, listId, total, orientation, t],\n )\n\n const getPrevTriggerProps: PropGetter<\"button\"> = useCallback(\n (props = {}) => ({\n \"aria-controls\": listId,\n \"aria-label\": t(\"Go to previous slide\"),\n \"data-orientation\": orientation,\n disabled: !carousel?.canScrollPrev(),\n ...props,\n onClick: handlerAll(props.onClick, () => carousel?.scrollPrev()),\n }),\n [carousel, listId, orientation, t],\n )\n\n const getNextTriggerProps: PropGetter<\"button\"> = useCallback(\n (props = {}) => ({\n \"aria-controls\": listId,\n \"aria-label\": t(\"Go to next slide\"),\n \"data-orientation\": orientation,\n disabled: !carousel?.canScrollNext(),\n ...props,\n onClick: handlerAll(props.onClick, () => carousel?.scrollNext()),\n }),\n [carousel, listId, orientation, t],\n )\n\n const getIndicatorsProps: PropGetter = useCallback(\n (props = {}) => ({\n \"aria-label\": t(\"Slides\"),\n \"aria-orientation\": orientation,\n role: \"tablist\",\n ...props,\n }),\n [orientation, t],\n )\n\n const getIndicatorProps: RequiredPropGetter<\"button\", { index: number }> =\n useCallback(\n ({ ref, index: indexProp, ...props }) => {\n const page = indexProp + 1\n const selected = index === indexProp\n\n return {\n type: \"button\" as const,\n \"aria-controls\": `${listId}-${indexProp}`,\n \"aria-label\": t(\"Go to {page} slide\", { page }),\n \"aria-selected\": selected,\n \"data-index\": indexProp.toString(),\n \"data-orientation\": orientation,\n \"data-selected\": dataAttr(selected),\n role: \"tab\",\n tabIndex: selected ? 0 : -1,\n ...props,\n ref: mergeRefs(ref, (node) => {\n indicatorMapRef.current.set(indexProp, node)\n }),\n onClick: handlerAll(props.onClick, () =>\n carousel?.scrollTo(indexProp),\n ),\n onKeyDown: handlerAll(props.onKeyDown, onKeyDown(indexProp)),\n }\n },\n [index, listId, t, orientation, onKeyDown, carousel],\n )\n\n return {\n carousel,\n index,\n setIndex,\n snapCount,\n total,\n getIndicatorProps,\n getIndicatorsProps,\n getItemProps,\n getListProps,\n getNextTriggerProps,\n getPrevTriggerProps,\n getRootProps,\n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;EACL;EACA;EACA;EACA;EACA;EACA,mBAlCA,aACG,EAAE,KAAK,OAAOH,aAAW,GAAG,YAAY;GACvC,MAAM,OAAOA,cAAY;GACzB,MAAM,WAAW,UAAUA;AAE3B,UAAO;IACL,MAAM;IACN,iBAAiB,GAAG,OAAO,GAAGA;IAC9B,cAAc,EAAE,sBAAsB,EAAE,MAAM,CAAC;IAC/C,iBAAiB;IACjB,cAAcA,YAAU,UAAU;IAClC,oBAAoB;IACpB,6CAA0B,SAAS;IACnC,MAAM;IACN,UAAU,WAAW,IAAI;IACzB,GAAG;IACH,KAAK,UAAU,MAAM,SAAS;AAC5B,qBAAgB,QAAQ,IAAIA,aAAW,KAAK;MAC5C;IACF,uCAAoB,MAAM,eACxB,UAAU,SAASA,YAAU,CAC9B;IACD,yCAAsB,MAAM,WAAW,UAAUA,YAAU,CAAC;IAC7D;KAEH;GAAC;GAAO;GAAQ;GAAG;GAAa;GAAW;GAAS,CACrD;EASD;EACA;EACA;EACA;EACA;EACA;EACD"}
|
|
1
|
+
{"version":3,"file":"use-carousel.js","names":["createContext","snapCount","total","index","getRootProps: PropGetter<\"section\">","getListProps: PropGetter","getItemProps: RequiredPropGetter<\"div\", { index: number }>","indexProp","getPrevTriggerProps: PropGetter<\"button\">","getNextTriggerProps: PropGetter<\"button\">","getIndicatorsProps: PropGetter"],"sources":["../../../../src/components/carousel/use-carousel.ts"],"sourcesContent":["\"use client\"\n\nimport type {\n EmblaCarouselType,\n EmblaOptionsType,\n EmblaPluginType,\n} from \"embla-carousel\"\nimport type { KeyboardEvent, RefObject } from \"react\"\nimport type {\n HTMLProps,\n Orientation,\n PropGetter,\n RequiredPropGetter,\n} from \"../../core\"\nimport useEmblaCarousel from \"embla-carousel-react\"\nimport { useCallback, useEffect, useRef, useState } from \"react\"\nimport { mergeProps } from \"../../core\"\nimport { useBoolean } from \"../../hooks/use-boolean\"\nimport { useControllableState } from \"../../hooks/use-controllable-state\"\nimport { useI18n } from \"../../providers/i18n-provider\"\nimport {\n assignRef,\n createContext,\n dataAttr,\n handlerAll,\n isFunction,\n mergeRefs,\n runKeyAction,\n useIds,\n useUpdateEffect,\n} from \"../../utils\"\n\ntype EmblaOptions = Required<EmblaOptionsType>\nexport type CarouselAlign = EmblaOptions[\"align\"]\nexport type CarouselContainScroll = EmblaOptions[\"containScroll\"]\nexport type CarouselInViewThreshold = EmblaOptions[\"inViewThreshold\"]\nexport type CarouselWatchDrag = EmblaOptions[\"watchDrag\"]\nexport type CarouselWatchResize = EmblaOptions[\"watchResize\"]\nexport type CarouselWatchSlides = EmblaOptions[\"watchSlides\"]\nexport type CarouselControl = EmblaCarouselType\nexport type CarouselPlugin = EmblaPluginType\n\nexport interface CarouselContext extends Omit<\n UseCarouselReturn,\n \"getRootProps\"\n> {}\n\nconst [CarouselContext, useCarouselContext] = createContext<CarouselContext>({\n name: \"CarouselContext\",\n})\n\nexport { CarouselContext, useCarouselContext }\n\nexport interface UseCarouselProps extends Omit<\n HTMLProps<\"section\">,\n \"onChange\"\n> {\n /**\n * The alignment of the carousel.\n *\n * @default 'center'\n */\n align?: CarouselAlign\n /**\n * If `true`, the carousel will be autoplay.\n *\n * @default false\n */\n autoplay?: boolean\n /**\n * Clear leading and trailing empty space that causes excessive scrolling.\n * Use trimSnaps to only use snap points that trigger scrolling or keepSnaps to keep them.\n *\n * @default false\n */\n containScroll?: CarouselContainScroll\n /**\n * Ref of the resizable item callback.\n */\n controlRef?: RefObject<CarouselControl | null>\n /**\n * The initial index of the carousel slide.\n *\n * @default 0\n */\n defaultIndex?: number\n /**\n * The number for the autoplay interval of the carousel.\n *\n * @default 4000\n */\n delay?: number\n /**\n * If `true`, momentum scrolling will be enabled.\n *\n * @default false\n */\n dragFree?: boolean\n /**\n * If `true`, carousel can be scrolled with mouse and touch interactions.\n *\n * @default true\n */\n draggable?: boolean\n /**\n * Set scroll duration when triggered by any of the API methods.\n * Higher numbers enables slower scrolling.\n * Drag interactions are not affected because duration is then determined by the drag force.\n *\n * @default 25\n */\n duration?: number\n /**\n * The index of the carousel slide.\n */\n index?: number\n /**\n * Choose a fraction representing the percentage portion of a slide that needs to be visible in order to be considered in view.\n *\n * @default 0\n */\n inViewThreshold?: CarouselInViewThreshold\n /**\n * If `true`, infinite looping.\n * Automatically falls back to false if slide content isn't enough to loop.\n *\n * @default true\n */\n loop?: boolean\n /**\n * The orientation of the carousel.\n *\n * @default 'horizontal'\n */\n orientation?: Orientation\n /**\n * Embla plugins to use.\n */\n plugins?: CarouselPlugin[]\n /**\n * If `true`, allow the carousel to skip scroll snaps if it's dragged vigorously.\n * Note that this option will be ignored if the dragFree option is set to true.\n *\n * @default false\n */\n skipSnaps?: boolean\n /**\n * The number of slides that should be scrolled with next or previous buttons.\n *\n * @default 1\n */\n slidesToScroll?: number\n /**\n * If `true`, autoplay will pause when the mouse entries over.\n *\n * @default true\n */\n stopMouseEnterAutoplay?: boolean\n /**\n * Enables for scrolling the carousel with mouse and touch interactions.\n * Set this to `false` to disable drag events or pass a custom callback to add your own drag logic.\n *\n * @default true\n */\n watchDrag?: CarouselWatchDrag\n /**\n * Embla automatically watches the container and slides for size changes and runs `reInit` when any size has changed.\n * Set this to `false` to disable this behaviour or pass a custom callback to add your own resize logic.\n *\n * @default true\n */\n watchResize?: CarouselWatchResize\n /**\n * Embla automatically watches the container for added and/or removed slides and runs `reInit` if needed.\n * Set this to `false` to disable this behaviour or pass a custom callback to add your own slides changed logic.\n *\n * @default true\n */\n watchSlides?: CarouselWatchSlides\n /**\n * The callback invoked when carousel slide selected.\n */\n onChange?: (index: number) => void\n /**\n * A callback that return the current scroll amount when the carousel is scrolled.\n */\n onScrollProgress?: (progress: number) => void\n}\n\nexport const useCarousel = ({\n id,\n align = \"center\",\n autoplay = false,\n containScroll = false,\n controlRef,\n defaultIndex = 0,\n delay = 4000,\n dragFree = false,\n draggable = true,\n duration = 25,\n index: indexProp,\n inViewThreshold = 0,\n loop = true,\n orientation = \"horizontal\",\n plugins = [],\n skipSnaps = false,\n slidesToScroll = 1,\n stopMouseEnterAutoplay = true,\n watchDrag = draggable,\n watchResize: watchResizeProp = true,\n watchSlides = true,\n onChange,\n onScrollProgress,\n ...rest\n}: UseCarouselProps = {}) => {\n const { t } = useI18n(\"carousel\")\n const [index, setIndex] = useControllableState({\n defaultValue: defaultIndex,\n value: indexProp,\n onChange,\n })\n const [rootId, listId] = useIds()\n const [hover, { off: onMouseLeave, on: onMouseEnter }] = useBoolean()\n const timeoutId = useRef<NodeJS.Timeout | null>(null)\n const indicatorMapRef = useRef<Map<number, HTMLButtonElement | null>>(\n new Map(),\n )\n const listRef = useRef<HTMLDivElement>(null)\n const horizontal = orientation === \"horizontal\"\n const axis = horizontal ? \"x\" : \"y\"\n const [snapCount, setSnapCount] = useState(0)\n const [total, setTotal] = useState(0)\n const watchResize = useCallback<Extract<CarouselWatchResize, Function>>(\n (methods, entries) => {\n const result = isFunction(watchResizeProp)\n ? watchResizeProp(methods, entries)\n : true\n const snapCount = methods.scrollSnapList().length\n const total = methods.slideNodes().length\n\n setSnapCount(snapCount)\n setTotal(total)\n\n return result\n },\n [watchResizeProp],\n )\n const [carouselRef, carousel] = useEmblaCarousel(\n {\n align,\n axis,\n container: listRef.current,\n containScroll,\n dragFree,\n duration,\n inViewThreshold,\n loop,\n skipSnaps,\n slidesToScroll,\n startIndex: defaultIndex,\n watchDrag,\n watchResize,\n watchSlides,\n },\n plugins,\n )\n\n id ??= rootId\n\n const onInit = useCallback((methods: CarouselControl) => {\n const snapCount = methods.scrollSnapList().length\n const total = methods.slideNodes().length\n\n setSnapCount(snapCount)\n setTotal(total)\n }, [])\n\n const onScroll = useCallback(() => {\n if (!carousel) return\n\n const progress = Math.round(\n Math.max(0, Math.min(1, carousel.scrollProgress())) * 100,\n )\n\n onScrollProgress?.(progress)\n }, [carousel, onScrollProgress])\n\n const onSelect = useCallback(() => {\n if (!carousel) return\n\n const index = carousel.selectedScrollSnap()\n\n setIndex(index)\n }, [carousel, setIndex])\n\n const onFocusIndicator = useCallback(\n (index: number) => {\n const el = indicatorMapRef.current.get(index)\n\n el?.focus()\n carousel?.scrollTo(index)\n },\n [carousel],\n )\n\n const onKeyDown = useCallback(\n (index: number) => (ev: KeyboardEvent<HTMLButtonElement>) => {\n const lastIndex = snapCount - 1\n\n runKeyAction(ev, {\n ArrowDown: () => {\n if (horizontal) return\n\n index = index === lastIndex ? 0 : index + 1\n\n onFocusIndicator(index)\n },\n ArrowLeft: () => {\n if (!horizontal) return\n\n index = index === 0 ? lastIndex : index - 1\n\n onFocusIndicator(index)\n },\n ArrowRight: () => {\n if (!horizontal) return\n\n index = index === lastIndex ? 0 : index + 1\n\n onFocusIndicator(index)\n },\n ArrowUp: () => {\n if (horizontal) return\n\n index = index === 0 ? lastIndex : index - 1\n\n onFocusIndicator(index)\n },\n End: () => {\n onFocusIndicator(lastIndex)\n },\n Home: () => {\n onFocusIndicator(0)\n },\n })\n },\n [snapCount, horizontal, onFocusIndicator],\n )\n\n assignRef(controlRef, carousel)\n\n useEffect(() => {\n if (carousel) {\n carousel.on(\"reInit\", onInit)\n carousel.on(\"select\", onSelect)\n carousel.on(\"scroll\", onScroll)\n\n onScroll()\n\n return () => {\n carousel.off(\"reInit\", onInit)\n carousel.off(\"select\", onSelect)\n carousel.off(\"scroll\", onScroll)\n }\n }\n }, [carousel, onInit, onScroll, onSelect])\n\n useEffect(() => {\n const stop = hover && stopMouseEnterAutoplay\n const last = !carousel?.canScrollNext()\n\n if (carousel && autoplay && !stop && !last) {\n timeoutId.current = setInterval(() => {\n carousel.scrollNext()\n }, delay)\n } else {\n if (timeoutId.current) clearInterval(timeoutId.current)\n\n timeoutId.current = null\n }\n\n return () => {\n if (timeoutId.current) clearInterval(timeoutId.current)\n }\n }, [autoplay, carousel, delay, hover, stopMouseEnterAutoplay])\n\n useUpdateEffect(() => {\n if (!carousel) return\n\n if (indexProp === undefined) return\n\n carousel.scrollTo(indexProp)\n }, [indexProp])\n\n useUpdateEffect(() => {\n if (!carousel) return\n\n carousel.reInit()\n }, [\n carousel,\n total,\n align,\n axis,\n containScroll,\n dragFree,\n duration,\n inViewThreshold,\n loop,\n skipSnaps,\n slidesToScroll,\n ])\n\n const getRootProps: PropGetter<\"section\"> = useCallback(\n ({ ref, ...props } = {}) =>\n mergeProps(\n {\n id,\n \"aria-roledescription\": \"carousel\",\n \"data-orientation\": orientation,\n },\n rest,\n {\n ref,\n onMouseEnter,\n onMouseLeave,\n },\n props,\n )(),\n [id, onMouseEnter, onMouseLeave, rest, orientation],\n )\n\n const getListProps: PropGetter = useCallback(\n ({ ref, ...props } = {}) => ({\n id: listId,\n \"aria-live\": autoplay ? \"off\" : \"polite\",\n \"data-orientation\": orientation,\n ...props,\n ref: mergeRefs(ref, listRef, carouselRef),\n }),\n [autoplay, listId, orientation, carouselRef],\n )\n\n const getItemProps: RequiredPropGetter<\"div\", { index: number }> =\n useCallback(\n ({ index: indexProp, ...props }) => {\n const page = indexProp + 1\n const selected = index === indexProp\n\n return {\n id: `${listId}-${indexProp}`,\n \"aria-label\": t(\"{page} of {total}\", { page, total }),\n \"aria-roledescription\": \"slide\",\n \"data-index\": indexProp.toString(),\n \"data-orientation\": orientation,\n \"data-selected\": dataAttr(selected),\n role: \"tabpanel\",\n ...props,\n }\n },\n [index, listId, total, orientation, t],\n )\n\n const getPrevTriggerProps: PropGetter<\"button\"> = useCallback(\n (props = {}) => ({\n \"aria-controls\": listId,\n \"aria-label\": t(\"Go to previous slide\"),\n \"data-orientation\": orientation,\n disabled: !carousel?.canScrollPrev(),\n ...props,\n onClick: handlerAll(props.onClick, () => carousel?.scrollPrev()),\n }),\n [carousel, listId, orientation, t],\n )\n\n const getNextTriggerProps: PropGetter<\"button\"> = useCallback(\n (props = {}) => ({\n \"aria-controls\": listId,\n \"aria-label\": t(\"Go to next slide\"),\n \"data-orientation\": orientation,\n disabled: !carousel?.canScrollNext(),\n ...props,\n onClick: handlerAll(props.onClick, () => carousel?.scrollNext()),\n }),\n [carousel, listId, orientation, t],\n )\n\n const getIndicatorsProps: PropGetter = useCallback(\n (props = {}) => ({\n \"aria-label\": t(\"Slides\"),\n \"aria-orientation\": orientation,\n role: \"tablist\",\n ...props,\n }),\n [orientation, t],\n )\n\n const getIndicatorProps: RequiredPropGetter<\"button\", { index: number }> =\n useCallback(\n ({ ref, index: indexProp, ...props }) => {\n const page = indexProp + 1\n const selected = index === indexProp\n\n return {\n type: \"button\" as const,\n \"aria-controls\": `${listId}-${indexProp}`,\n \"aria-label\": t(\"Go to {page} slide\", { page }),\n \"aria-selected\": selected,\n \"data-index\": indexProp.toString(),\n \"data-orientation\": orientation,\n \"data-selected\": dataAttr(selected),\n role: \"tab\",\n tabIndex: selected ? 0 : -1,\n ...props,\n ref: mergeRefs(ref, (node) => {\n indicatorMapRef.current.set(indexProp, node)\n }),\n onClick: handlerAll(props.onClick, () =>\n carousel?.scrollTo(indexProp),\n ),\n onKeyDown: handlerAll(props.onKeyDown, onKeyDown(indexProp)),\n }\n },\n [index, listId, t, orientation, onKeyDown, carousel],\n )\n\n return {\n carousel,\n index,\n setIndex,\n snapCount,\n total,\n getIndicatorProps,\n getIndicatorsProps,\n getItemProps,\n getListProps,\n getNextTriggerProps,\n getPrevTriggerProps,\n getRootProps,\n }\n}\n\nexport type UseCarouselReturn = ReturnType<typeof useCarousel>\n"],"mappings":";;;;;;;;;;;;;;;;;AA+CA,MAAM,CAAC,iBAAiB,sBAAsBA,gBAA+B,EAC3E,MAAM,mBACP,CAAC;AA4IF,MAAa,eAAe,EAC1B,IACA,QAAQ,UACR,WAAW,OACX,gBAAgB,OAChB,YACA,eAAe,GACf,QAAQ,KACR,WAAW,OACX,YAAY,MACZ,WAAW,IACX,OAAO,WACP,kBAAkB,GAClB,OAAO,MACP,cAAc,cACd,UAAU,EAAE,EACZ,YAAY,OACZ,iBAAiB,GACjB,yBAAyB,MACzB,YAAY,WACZ,aAAa,kBAAkB,MAC/B,cAAc,MACd,UACA,kBACA,GAAG,SACiB,EAAE,KAAK;CAC3B,MAAM,EAA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GAAQ;GAAG;GAAa;GAAW;GAAS,CACrD;EASD;EACA;EACA;EACA;EACA;EACA;EACD"}
|
|
@@ -5,6 +5,8 @@ import { runKeyAction } from "../../utils/dom.js";
|
|
|
5
5
|
import { useUpdateEffect } from "../../utils/effect.js";
|
|
6
6
|
import { mergeRefs } from "../../utils/ref.js";
|
|
7
7
|
import { utils_exports } from "../../utils/index.js";
|
|
8
|
+
import { mergeProps } from "../../core/components/props.js";
|
|
9
|
+
import "../../core/index.js";
|
|
8
10
|
import { useControllableState } from "../../hooks/use-controllable-state/index.js";
|
|
9
11
|
import { createDescendants } from "../../hooks/use-descendants/index.js";
|
|
10
12
|
import { useCallback, useId, useState } from "react";
|
|
@@ -62,12 +64,7 @@ const useTabs = ({ id, defaultIndex = 0, index: indexProp, manual = false, orien
|
|
|
62
64
|
setIndex(indexProp);
|
|
63
65
|
setFocusedIndex(indexProp);
|
|
64
66
|
}, [indexProp]);
|
|
65
|
-
const getRootProps = useCallback(({ ref, ...props } = {}) => ({
|
|
66
|
-
"data-orientation": orientation,
|
|
67
|
-
...rest,
|
|
68
|
-
...props,
|
|
69
|
-
ref: mergeRefs(ref, rest.ref)
|
|
70
|
-
}), [orientation, rest]);
|
|
67
|
+
const getRootProps = useCallback(({ ref, ...props } = {}) => mergeProps({ "data-orientation": orientation }, rest, props, { ref })(), [orientation, rest]);
|
|
71
68
|
const getListProps = useCallback((props = {}) => ({
|
|
72
69
|
"aria-orientation": orientation,
|
|
73
70
|
role: "tablist",
|
|
@@ -115,7 +112,7 @@ const useTab = ({ id, disabled, index, ...rest }) => {
|
|
|
115
112
|
return {
|
|
116
113
|
index,
|
|
117
114
|
selected,
|
|
118
|
-
getRootProps: useCallback(({ ref, ...props } = {}) => ({
|
|
115
|
+
getRootProps: useCallback(({ ref, ...props } = {}) => mergeProps({
|
|
119
116
|
id,
|
|
120
117
|
type: "button",
|
|
121
118
|
"aria-controls": tabPanelId,
|
|
@@ -123,13 +120,11 @@ const useTab = ({ id, disabled, index, ...rest }) => {
|
|
|
123
120
|
"data-orientation": orientation,
|
|
124
121
|
disabled,
|
|
125
122
|
role: "tab",
|
|
126
|
-
tabIndex: selected ? 0 : -1
|
|
127
|
-
|
|
128
|
-
...props,
|
|
129
|
-
ref: mergeRefs(ref, register),
|
|
123
|
+
tabIndex: selected ? 0 : -1
|
|
124
|
+
}, rest, props, { ref: mergeRefs(ref, register) }, {
|
|
130
125
|
onClick: (0, utils_exports.handlerAll)(props.onClick, rest.onClick, onClick),
|
|
131
126
|
onFocus: (0, utils_exports.handlerAll)(props.onFocus, rest.onFocus, onFocus)
|
|
132
|
-
}), [
|
|
127
|
+
})({ mergeEvent: false }), [
|
|
133
128
|
disabled,
|
|
134
129
|
id,
|
|
135
130
|
onClick,
|
|
@@ -151,17 +146,14 @@ const useTabPanel = ({ id, "aria-labelledby": ariaLabelledbyProp, index, ...rest
|
|
|
151
146
|
return {
|
|
152
147
|
index,
|
|
153
148
|
selected,
|
|
154
|
-
getRootProps: useCallback(({ ref, "aria-labelledby": ariaLabelledby, ...props } = {}) => ({
|
|
149
|
+
getRootProps: useCallback(({ ref, "aria-labelledby": ariaLabelledby, ...props } = {}) => mergeProps({
|
|
155
150
|
id,
|
|
156
151
|
"aria-labelledby": (0, utils_exports.cx)(ariaLabelledbyProp, ariaLabelledby, tabId),
|
|
157
152
|
"data-orientation": orientation,
|
|
158
153
|
hidden: !selected,
|
|
159
154
|
role: "tabpanel",
|
|
160
|
-
tabIndex: selected ? 0 : -1
|
|
161
|
-
|
|
162
|
-
...props,
|
|
163
|
-
ref: mergeRefs(ref, register)
|
|
164
|
-
}), [
|
|
155
|
+
tabIndex: selected ? 0 : -1
|
|
156
|
+
}, rest, props, { ref: mergeRefs(ref, register) })(), [
|
|
165
157
|
id,
|
|
166
158
|
ariaLabelledbyProp,
|
|
167
159
|
orientation,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-tabs.js","names":["createContext","getRootProps: PropGetter","getListProps: PropGetter"],"sources":["../../../../src/components/tabs/use-tabs.ts"],"sourcesContent":["\"use client\"\n\nimport type { KeyboardEvent } from \"react\"\nimport type { HTMLProps, Orientation, PropGetter } from \"../../core\"\nimport { useCallback, useId, useState } from \"react\"\nimport { useControllableState } from \"../../hooks/use-controllable-state\"\nimport { createDescendants } from \"../../hooks/use-descendants\"\nimport {\n createContext,\n cx,\n handlerAll,\n isUndefined,\n mergeRefs,\n runKeyAction,\n useUpdateEffect,\n} from \"../../utils\"\n\nconst {\n DescendantsContext: TabDescendantsContext,\n useDescendant: useTabDescendant,\n useDescendants: useTabDescendants,\n} = createDescendants<HTMLButtonElement>()\n\nexport { TabDescendantsContext, useTabDescendant, useTabDescendants }\n\nconst {\n DescendantsContext: TabPanelDescendantsContext,\n useDescendant: useTabPanelDescendant,\n useDescendants: useTabPanelDescendants,\n} = createDescendants<HTMLDivElement>()\n\nexport {\n TabPanelDescendantsContext,\n useTabPanelDescendant,\n useTabPanelDescendants,\n}\n\ninterface TabsContext extends Omit<\n UseTabsReturn,\n \"getRootProps\" | \"tabDescendants\" | \"tabPanelDescendants\"\n> {}\n\nconst [TabsContext, useTabsContext] = createContext<TabsContext>({\n name: \"TabsContext\",\n})\n\nexport { TabsContext, useTabsContext }\n\nexport interface UseTabsProps extends Omit<HTMLProps, \"onChange\"> {\n /**\n * The index of the selected tab.\n */\n defaultIndex?: number\n /**\n * The index of the selected tab.\n */\n index?: number\n /**\n * If `true`, the tabs will be manually activated and display its panel by pressing Space or Enter.\n *\n * If `false`, the tabs will be automatically activated and their panel is displayed when they receive focus.\n *\n * @default false\n */\n manual?: boolean\n /**\n * The orientation of the tabs.\n *\n * @default 'horizontal'\n */\n orientation?: Orientation\n /**\n * The callback invoked when the index changes.\n */\n onChange?: (index: number) => void\n}\n\nexport const useTabs = ({\n id,\n defaultIndex = 0,\n index: indexProp,\n manual = false,\n orientation = \"horizontal\",\n onChange,\n ...rest\n}: UseTabsProps = {}) => {\n const uuid = useId()\n const tabDescendants = useTabDescendants()\n const tabPanelDescendants = useTabPanelDescendants()\n const [index, setIndex] = useControllableState({\n defaultValue: defaultIndex,\n value: indexProp,\n onChange,\n })\n const [focusedIndex, setFocusedIndex] = useState<number>(index)\n const horizontal = orientation === \"horizontal\"\n\n id ??= uuid\n\n const onFocusFirstTab = useCallback(() => {\n const first = tabDescendants.enabledFirstValue()\n\n if (first) first.node.focus()\n }, [tabDescendants])\n\n const onFocusLastTab = useCallback(() => {\n const last = tabDescendants.enabledLastValue()\n\n if (last) last.node.focus()\n }, [tabDescendants])\n\n const onFocusNextTab = useCallback(() => {\n const next = tabDescendants.enabledNextValue(focusedIndex)\n\n if (next) next.node.focus()\n }, [tabDescendants, focusedIndex])\n\n const onFocusPrevTab = useCallback(() => {\n const prev = tabDescendants.enabledPrevValue(focusedIndex)\n\n if (prev) prev.node.focus()\n }, [tabDescendants, focusedIndex])\n\n const onKeyDown = useCallback(\n (ev: KeyboardEvent<HTMLDivElement>) => {\n runKeyAction(ev, {\n ArrowDown: !horizontal ? onFocusNextTab : undefined,\n ArrowLeft: horizontal ? onFocusPrevTab : undefined,\n ArrowRight: horizontal ? onFocusNextTab : undefined,\n ArrowUp: !horizontal ? onFocusPrevTab : undefined,\n End: onFocusLastTab,\n Home: onFocusFirstTab,\n })\n },\n [\n horizontal,\n onFocusNextTab,\n onFocusPrevTab,\n onFocusLastTab,\n onFocusFirstTab,\n ],\n )\n\n useUpdateEffect(() => {\n if (isUndefined(indexProp)) return\n\n setIndex(indexProp)\n setFocusedIndex(indexProp)\n }, [indexProp])\n\n const getRootProps: PropGetter = useCallback(\n ({ ref, ...props } = {}) => ({\n \"data-orientation\": orientation,\n ...rest,\n ...props,\n ref: mergeRefs(ref, rest.ref),\n }),\n [orientation, rest],\n )\n\n const getListProps: PropGetter = useCallback(\n (props = {}) => ({\n \"aria-orientation\": orientation,\n role: \"tablist\",\n ...props,\n onKeyDown: handlerAll(props.onKeyDown, onKeyDown),\n }),\n [orientation, onKeyDown],\n )\n\n return {\n id,\n focusedIndex,\n index,\n manual,\n orientation,\n setFocusedIndex,\n setIndex,\n tabDescendants,\n tabPanelDescendants,\n getListProps,\n getRootProps,\n }\n}\n\nexport type UseTabsReturn = ReturnType<typeof useTabs>\n\nexport interface UseTabProps extends HTMLProps<\"button\"> {\n /**\n * The index of the tab.\n */\n index: number\n}\n\nexport const useTab = ({ id, disabled, index, ...rest }: UseTabProps) => {\n const {\n id: rootId,\n index: selectedIndex,\n manual,\n orientation,\n setFocusedIndex,\n setIndex,\n } = useTabsContext()\n const { register } = useTabDescendant({ disabled })\n const tabPanelId = `${rootId}-panel-${index}`\n const selected = index === selectedIndex\n\n id ??= `${rootId}-tab-${index}`\n\n const onClick = useCallback(() => {\n if (!disabled) setIndex(index)\n }, [index, setIndex, disabled])\n\n const onFocus = useCallback(() => {\n if (disabled) return\n\n setFocusedIndex(index)\n\n if (!manual) setIndex(index)\n }, [setFocusedIndex, index, manual, disabled, setIndex])\n\n const getRootProps: PropGetter<\"button\"> = useCallback(\n ({ ref, ...props } = {}) => ({\n id,\n type: \"button\",\n \"aria-controls\": tabPanelId,\n \"aria-selected\": selected,\n \"data-orientation\": orientation,\n disabled,\n role: \"tab\",\n tabIndex: selected ? 0 : -1,\n ...rest,\n ...props,\n ref: mergeRefs(ref, register),\n onClick: handlerAll(props.onClick, rest.onClick, onClick),\n onFocus: handlerAll(props.onFocus, rest.onFocus, onFocus),\n }),\n [\n disabled,\n id,\n onClick,\n onFocus,\n orientation,\n register,\n rest,\n selected,\n tabPanelId,\n ],\n )\n\n return { index, selected, getRootProps }\n}\n\nexport type UseTabReturn = ReturnType<typeof useTab>\n\nexport interface UseTabPanelProps extends HTMLProps {\n /**\n * The index of the tab panel.\n */\n index: number\n}\n\nexport const useTabPanel = ({\n id,\n \"aria-labelledby\": ariaLabelledbyProp,\n index,\n ...rest\n}: UseTabPanelProps) => {\n const { id: rootId, index: selectedIndex, orientation } = useTabsContext()\n const { register } = useTabPanelDescendant()\n const tabId = `${rootId}-tab-${index}`\n const selected = index === selectedIndex\n\n id ??= `${rootId}-panel-${index}`\n\n const getRootProps: PropGetter = useCallback(\n ({ ref, \"aria-labelledby\": ariaLabelledby, ...props } = {}) => ({\n id,\n \"aria-labelledby\": cx(ariaLabelledbyProp, ariaLabelledby, tabId),\n \"data-orientation\": orientation,\n hidden: !selected,\n role: \"tabpanel\",\n tabIndex: selected ? 0 : -1,\n ...rest,\n ...props,\n ref: mergeRefs(ref, register),\n }),\n [id, ariaLabelledbyProp, orientation, register, rest, selected, tabId],\n )\n\n return { index, selected, getRootProps }\n}\n\nexport type UseTabPanelReturn = ReturnType<typeof useTabPanel>\n"],"mappings":";;;;;;;;;;;;AAiBA,MAAM,EACJ,oBAAoB,uBACpB,eAAe,kBACf,gBAAgB,sBACd,mBAAsC;AAI1C,MAAM,EACJ,oBAAoB,4BACpB,eAAe,uBACf,gBAAgB,2BACd,mBAAmC;AAavC,MAAM,CAAC,aAAa,kBAAkBA,gBAA2B,EAC/D,MAAM,eACP,CAAC;AAiCF,MAAa,WAAW,EACtB,IACA,eAAe,GACf,OAAO,WACP,SAAS,OACT,cAAc,cACd,UACA,GAAG,SACa,EAAE,KAAK;CACvB,MAAM,OAAO,OAAO;CACpB,MAAM,iBAAiB,mBAAmB;CAC1C,MAAM,sBAAsB,wBAAwB;CACpD,MAAM,CAAC,OAAO,YAAY,qBAAqB;EAC7C,cAAc;EACd,OAAO;EACP;EACD,CAAC;CACF,MAAM,CAAC,cAAc,mBAAmB,SAAiB,MAAM;CAC/D,MAAM,aAAa,gBAAgB;AAEnC,QAAO;CAEP,MAAM,kBAAkB,kBAAkB;EACxC,MAAM,QAAQ,eAAe,mBAAmB;AAEhD,MAAI,MAAO,OAAM,KAAK,OAAO;IAC5B,CAAC,eAAe,CAAC;CAEpB,MAAM,iBAAiB,kBAAkB;EACvC,MAAM,OAAO,eAAe,kBAAkB;AAE9C,MAAI,KAAM,MAAK,KAAK,OAAO;IAC1B,CAAC,eAAe,CAAC;CA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AC;GAAI;GAAoB;GAAa;GAAU;GAAM;GAAU;GAAM,CACvE;EAEuC"}
|
|
1
|
+
{"version":3,"file":"use-tabs.js","names":["createContext","getRootProps: PropGetter","getListProps: PropGetter"],"sources":["../../../../src/components/tabs/use-tabs.ts"],"sourcesContent":["\"use client\"\n\nimport type { KeyboardEvent } from \"react\"\nimport type { HTMLProps, Orientation, PropGetter } from \"../../core\"\nimport { useCallback, useId, useState } from \"react\"\nimport { mergeProps } from \"../../core\"\nimport { useControllableState } from \"../../hooks/use-controllable-state\"\nimport { createDescendants } from \"../../hooks/use-descendants\"\nimport {\n createContext,\n cx,\n handlerAll,\n isUndefined,\n mergeRefs,\n runKeyAction,\n useUpdateEffect,\n} from \"../../utils\"\n\nconst {\n DescendantsContext: TabDescendantsContext,\n useDescendant: useTabDescendant,\n useDescendants: useTabDescendants,\n} = createDescendants<HTMLButtonElement>()\n\nexport { TabDescendantsContext, useTabDescendant, useTabDescendants }\n\nconst {\n DescendantsContext: TabPanelDescendantsContext,\n useDescendant: useTabPanelDescendant,\n useDescendants: useTabPanelDescendants,\n} = createDescendants<HTMLDivElement>()\n\nexport {\n TabPanelDescendantsContext,\n useTabPanelDescendant,\n useTabPanelDescendants,\n}\n\ninterface TabsContext extends Omit<\n UseTabsReturn,\n \"getRootProps\" | \"tabDescendants\" | \"tabPanelDescendants\"\n> {}\n\nconst [TabsContext, useTabsContext] = createContext<TabsContext>({\n name: \"TabsContext\",\n})\n\nexport { TabsContext, useTabsContext }\n\nexport interface UseTabsProps extends Omit<HTMLProps, \"onChange\"> {\n /**\n * The index of the selected tab.\n */\n defaultIndex?: number\n /**\n * The index of the selected tab.\n */\n index?: number\n /**\n * If `true`, the tabs will be manually activated and display its panel by pressing Space or Enter.\n *\n * If `false`, the tabs will be automatically activated and their panel is displayed when they receive focus.\n *\n * @default false\n */\n manual?: boolean\n /**\n * The orientation of the tabs.\n *\n * @default 'horizontal'\n */\n orientation?: Orientation\n /**\n * The callback invoked when the index changes.\n */\n onChange?: (index: number) => void\n}\n\nexport const useTabs = ({\n id,\n defaultIndex = 0,\n index: indexProp,\n manual = false,\n orientation = \"horizontal\",\n onChange,\n ...rest\n}: UseTabsProps = {}) => {\n const uuid = useId()\n const tabDescendants = useTabDescendants()\n const tabPanelDescendants = useTabPanelDescendants()\n const [index, setIndex] = useControllableState({\n defaultValue: defaultIndex,\n value: indexProp,\n onChange,\n })\n const [focusedIndex, setFocusedIndex] = useState<number>(index)\n const horizontal = orientation === \"horizontal\"\n\n id ??= uuid\n\n const onFocusFirstTab = useCallback(() => {\n const first = tabDescendants.enabledFirstValue()\n\n if (first) first.node.focus()\n }, [tabDescendants])\n\n const onFocusLastTab = useCallback(() => {\n const last = tabDescendants.enabledLastValue()\n\n if (last) last.node.focus()\n }, [tabDescendants])\n\n const onFocusNextTab = useCallback(() => {\n const next = tabDescendants.enabledNextValue(focusedIndex)\n\n if (next) next.node.focus()\n }, [tabDescendants, focusedIndex])\n\n const onFocusPrevTab = useCallback(() => {\n const prev = tabDescendants.enabledPrevValue(focusedIndex)\n\n if (prev) prev.node.focus()\n }, [tabDescendants, focusedIndex])\n\n const onKeyDown = useCallback(\n (ev: KeyboardEvent<HTMLDivElement>) => {\n runKeyAction(ev, {\n ArrowDown: !horizontal ? onFocusNextTab : undefined,\n ArrowLeft: horizontal ? onFocusPrevTab : undefined,\n ArrowRight: horizontal ? onFocusNextTab : undefined,\n ArrowUp: !horizontal ? onFocusPrevTab : undefined,\n End: onFocusLastTab,\n Home: onFocusFirstTab,\n })\n },\n [\n horizontal,\n onFocusNextTab,\n onFocusPrevTab,\n onFocusLastTab,\n onFocusFirstTab,\n ],\n )\n\n useUpdateEffect(() => {\n if (isUndefined(indexProp)) return\n\n setIndex(indexProp)\n setFocusedIndex(indexProp)\n }, [indexProp])\n\n const getRootProps: PropGetter = useCallback(\n ({ ref, ...props } = {}) =>\n mergeProps({ \"data-orientation\": orientation }, rest, props, { ref })(),\n [orientation, rest],\n )\n\n const getListProps: PropGetter = useCallback(\n (props = {}) => ({\n \"aria-orientation\": orientation,\n role: \"tablist\",\n ...props,\n onKeyDown: handlerAll(props.onKeyDown, onKeyDown),\n }),\n [orientation, onKeyDown],\n )\n\n return {\n id,\n focusedIndex,\n index,\n manual,\n orientation,\n setFocusedIndex,\n setIndex,\n tabDescendants,\n tabPanelDescendants,\n getListProps,\n getRootProps,\n }\n}\n\nexport type UseTabsReturn = ReturnType<typeof useTabs>\n\nexport interface UseTabProps extends HTMLProps<\"button\"> {\n /**\n * The index of the tab.\n */\n index: number\n}\n\nexport const useTab = ({ id, disabled, index, ...rest }: UseTabProps) => {\n const {\n id: rootId,\n index: selectedIndex,\n manual,\n orientation,\n setFocusedIndex,\n setIndex,\n } = useTabsContext()\n const { register } = useTabDescendant({ disabled })\n const tabPanelId = `${rootId}-panel-${index}`\n const selected = index === selectedIndex\n\n id ??= `${rootId}-tab-${index}`\n\n const onClick = useCallback(() => {\n if (!disabled) setIndex(index)\n }, [index, setIndex, disabled])\n\n const onFocus = useCallback(() => {\n if (disabled) return\n\n setFocusedIndex(index)\n\n if (!manual) setIndex(index)\n }, [setFocusedIndex, index, manual, disabled, setIndex])\n\n const getRootProps: PropGetter<\"button\"> = useCallback(\n ({ ref, ...props } = {}) =>\n mergeProps(\n {\n id,\n type: \"button\",\n \"aria-controls\": tabPanelId,\n \"aria-selected\": selected,\n \"data-orientation\": orientation,\n disabled,\n role: \"tab\",\n tabIndex: selected ? 0 : -1,\n },\n rest,\n props,\n { ref: mergeRefs(ref, register) },\n {\n onClick: handlerAll(props.onClick, rest.onClick, onClick),\n onFocus: handlerAll(props.onFocus, rest.onFocus, onFocus),\n },\n )({ mergeEvent: false }),\n [\n disabled,\n id,\n onClick,\n onFocus,\n orientation,\n register,\n rest,\n selected,\n tabPanelId,\n ],\n )\n\n return { index, selected, getRootProps }\n}\n\nexport type UseTabReturn = ReturnType<typeof useTab>\n\nexport interface UseTabPanelProps extends HTMLProps {\n /**\n * The index of the tab panel.\n */\n index: number\n}\n\nexport const useTabPanel = ({\n id,\n \"aria-labelledby\": ariaLabelledbyProp,\n index,\n ...rest\n}: UseTabPanelProps) => {\n const { id: rootId, index: selectedIndex, orientation } = useTabsContext()\n const { register } = useTabPanelDescendant()\n const tabId = `${rootId}-tab-${index}`\n const selected = index === selectedIndex\n\n id ??= `${rootId}-panel-${index}`\n\n const getRootProps: PropGetter = useCallback(\n ({ ref, \"aria-labelledby\": ariaLabelledby, ...props } = {}) =>\n mergeProps(\n {\n id,\n \"aria-labelledby\": cx(ariaLabelledbyProp, ariaLabelledby, tabId),\n \"data-orientation\": orientation,\n hidden: !selected,\n role: \"tabpanel\",\n tabIndex: selected ? 0 : -1,\n },\n rest,\n props,\n { ref: mergeRefs(ref, register) },\n )(),\n [id, ariaLabelledbyProp, orientation, register, rest, selected, tabId],\n )\n\n return { index, selected, getRootProps }\n}\n\nexport type UseTabPanelReturn = ReturnType<typeof useTabPanel>\n"],"mappings":";;;;;;;;;;;;;;AAkBA,MAAM,EACJ,oBAAoB,uBACpB,eAAe,kBACf,gBAAgB,sBACd,mBAAsC;AAI1C,MAAM,EACJ,oBAAoB,4BACpB,eAAe,uBACf,gBAAgB,2BACd,mBAAmC;AAavC,MAAM,CAAC,aAAa,kBAAkBA,gBAA2B,EAC/D,MAAM,eACP,CAAC;AAiCF,MAAa,WAAW,EACtB,IACA,eAAe,GACf,OAAO,WACP,SAAS,OACT,cAAc,cACd,UACA,GAAG,SACa,EAAE,KAAK;CACvB,MAAM,OAAO,OAAO;CACpB,MAAM,iBAAiB,mBAAmB;CAC1C,MAAM,sBAAsB,wBAAwB;CACpD,MAAM,CAAC,OAAO,YAAY,qBAAqB;EAC7C,cAAc;EACd,OAAO;EACP;EACD,CAAC;CACF,MAAM,CAAC,cAAc,mBAAmB,SAAiB,MAAM;CAC/D,MAAM,aAAa,gBAAgB;AAEnC,QAAO;CAEP,MAAM,kBAAkB,kBAAkB;EACxC,MAAM,QAAQ,eAAe,mBAAmB;AAEhD,MA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K,mBAAmB,gBAAgB,GAAG,UAAU,EAAE,KACxD,WACE;GACE;GACA,yCAAsB,oBAAoB,gBAAgB,MAAM;GAChE,oBAAoB;GACpB,QAAQ,CAAC;GACT,MAAM;GACN,UAAU,WAAW,IAAI;GAC1B,EACD,MACA,OACA,EAAE,KAAK,UAAU,KAAK,SAAS,EAAE,CAClC,EAAE,EACL;GAAC;GAAI;GAAoB;GAAa;GAAU;GAAM;GAAU;GAAM,CACvE;EAEuC"}
|
|
@@ -9,7 +9,7 @@ import "../collapse/index.js";
|
|
|
9
9
|
import "../../index.js";
|
|
10
10
|
import { AccordionStyle } from "./accordion.style.js";
|
|
11
11
|
import { UseAccordionItemProps, UseAccordionProps } from "./use-accordion.js";
|
|
12
|
-
import * as
|
|
12
|
+
import * as react469 from "react";
|
|
13
13
|
|
|
14
14
|
//#region src/components/accordion/accordion.d.ts
|
|
15
15
|
interface AccordionCallBackProps {
|
|
@@ -33,7 +33,7 @@ interface AccordionRootProps extends Omit<HTMLStyledProps, "onChange">, ThemePro
|
|
|
33
33
|
*/
|
|
34
34
|
items?: AccordionItem[];
|
|
35
35
|
}
|
|
36
|
-
declare const AccordionPropsContext:
|
|
36
|
+
declare const AccordionPropsContext: react469.Context<Partial<AccordionRootProps> | undefined>, useAccordionPropsContext: () => Partial<AccordionRootProps> | undefined;
|
|
37
37
|
/**
|
|
38
38
|
* `Accordion` is a component for a list that displays information in an expandable or collapsible manner.
|
|
39
39
|
*
|
|
@@ -3,7 +3,7 @@ import { CSSModifierObject, CSSPropObject, CSSSlotObject } from "../../core/css/
|
|
|
3
3
|
import "../../index.js";
|
|
4
4
|
|
|
5
5
|
//#region src/components/accordion/accordion.style.d.ts
|
|
6
|
-
declare const accordionStyle: ComponentSlotStyle<"button" | "panel" | "icon" | "
|
|
6
|
+
declare const accordionStyle: ComponentSlotStyle<"button" | "panel" | "icon" | "item" | "root", CSSPropObject<CSSSlotObject<"button" | "panel" | "icon" | "item" | "root">>, CSSModifierObject<CSSSlotObject<"button" | "panel" | "icon" | "item" | "root">>, {
|
|
7
7
|
panel: {
|
|
8
8
|
button: {
|
|
9
9
|
rounded: "l2";
|