@ycparak/spring-carousel 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,158 @@
1
+ import * as react from 'react';
2
+ import { ComponentProps } from 'react';
3
+
4
+ type ProgressListener = (progress: number) => void;
5
+ /**
6
+ * The carousel's continuous position, in slides: 0 is the first slide, 1.5 is
7
+ * halfway between the second and third. It changes every animation frame without
8
+ * re-rendering React; read it with `get()` or listen with `subscribe()`.
9
+ */
10
+ interface CarouselProgress {
11
+ get(): number;
12
+ /** Slides per second. */
13
+ getVelocity(): number;
14
+ /** Calls `listener` immediately with the current value, then on every change. */
15
+ subscribe(listener: ProgressListener): () => void;
16
+ }
17
+
18
+ /**
19
+ * Physical spring parameters. Defaults reproduce the original Kami carousel.
20
+ */
21
+ interface PhysicalSpring {
22
+ stiffness?: number;
23
+ damping?: number;
24
+ mass?: number;
25
+ duration?: never;
26
+ bounce?: never;
27
+ }
28
+ /**
29
+ * Perceptual spring parameters, as in SwiftUI and Motion's `visualDuration`.
30
+ * `duration` is in seconds; `bounce` runs from -1 (overdamped) through 0 (critically
31
+ * damped) to 1 (undamped).
32
+ */
33
+ interface PerceptualSpring {
34
+ duration: number;
35
+ bounce?: number;
36
+ stiffness?: never;
37
+ damping?: never;
38
+ mass?: never;
39
+ }
40
+ type SpringOptions = PhysicalSpring | PerceptualSpring;
41
+
42
+ type KeyboardMode = 'focus' | 'global' | false;
43
+ type ReducedMotionMode = 'user' | 'always' | 'never';
44
+ interface NavigateOptions {
45
+ /** Set to false to move instantly. Defaults to true. */
46
+ animate?: boolean;
47
+ /** Starting velocity in slides per second. Defaults to the current velocity. */
48
+ velocity?: number;
49
+ }
50
+
51
+ interface SlideContextValue {
52
+ index: number;
53
+ isActive: boolean;
54
+ }
55
+
56
+ interface CarouselApi {
57
+ /** The slide the carousel is on, or heading to. */
58
+ index: number;
59
+ /** How many slides are mounted. 0 until the first client render. */
60
+ count: number;
61
+ /** True while a pointer is dragging the slides. */
62
+ isDragging: boolean;
63
+ canPrev: boolean;
64
+ canNext: boolean;
65
+ goTo: (index: number, options?: NavigateOptions) => void;
66
+ next: (options?: NavigateOptions) => void;
67
+ prev: (options?: NavigateOptions) => void;
68
+ /** The continuous position, updated every frame without re-rendering. */
69
+ progress: CarouselProgress;
70
+ }
71
+ /**
72
+ * Reads the carousel's state and controls from any component inside
73
+ * `<Carousel.Root>`. Re-renders when the index, count or drag state changes, never
74
+ * per frame; use `useCarouselProgress` for per-frame work.
75
+ */
76
+ declare function useCarousel(): CarouselApi;
77
+ /**
78
+ * Calls `listener` with the carousel's position on every frame it moves, and once
79
+ * on mount. Use it to drive styles imperatively (captions, parallax, progress bars)
80
+ * without re-rendering. The listener always sees the latest props and state.
81
+ */
82
+ declare function useCarouselProgress(listener: ProgressListener): void;
83
+ /** Reads the enclosing `<Carousel.Slide>`'s index and whether it is the current slide. */
84
+ declare function useSlide(): SlideContextValue;
85
+
86
+ /** How present an item is at a given progress: 1 when it is the current slide, 0 a slide or more away. */
87
+ declare function presence(progress: number, index: number): number;
88
+
89
+ interface RootProps extends ComponentProps<'section'> {
90
+ /** The current slide, for a controlled carousel. Pair with `onIndexChange`. */
91
+ index?: number;
92
+ /** The slide to start on, for an uncontrolled carousel. Defaults to 0. */
93
+ defaultIndex?: number;
94
+ /** Called when a gesture, key, tab or button asks for a new slide. */
95
+ onIndexChange?: (index: number) => void;
96
+ /**
97
+ * The spring that moves the carousel: `{ stiffness, damping, mass }` or
98
+ * `{ duration, bounce }` with `duration` in seconds.
99
+ * Defaults to `{ stiffness: 90, damping: 18.1, mass: 0.9 }`.
100
+ */
101
+ spring?: SpringOptions;
102
+ /**
103
+ * Where arrow keys move the carousel: `"focus"` when focus is inside it, `"global"`
104
+ * anywhere on the page outside text fields and other arrow-key widgets, or `false`.
105
+ * Defaults to `"focus"`.
106
+ */
107
+ keyboard?: KeyboardMode;
108
+ /** Lets horizontal trackpad swipes page through slides. Defaults to true. */
109
+ wheel?: boolean;
110
+ /** Lets pointers drag the slides. Defaults to true. */
111
+ draggable?: boolean;
112
+ /**
113
+ * `"user"` follows `prefers-reduced-motion`, `"always"` moves instantly and
114
+ * `"never"` always animates. Defaults to `"user"`.
115
+ */
116
+ reducedMotion?: ReducedMotionMode;
117
+ }
118
+ declare function CarouselRoot({ index: indexProp, defaultIndex, onIndexChange, spring, keyboard, wheel, draggable, reducedMotion, ref, style, children, ...props }: RootProps): react.JSX.Element;
119
+ type ViewportProps = ComponentProps<'div'>;
120
+ declare function CarouselViewport({ ref, ...props }: ViewportProps): react.JSX.Element;
121
+ type TrackProps = ComponentProps<'div'>;
122
+ declare function CarouselTrack({ ref, ...props }: TrackProps): react.JSX.Element;
123
+ interface SlideProps extends ComponentProps<'div'> {
124
+ /** This slide's position, from 0. Indexes must run 0, 1, 2… without gaps. */
125
+ index: number;
126
+ }
127
+ declare function CarouselSlide({ index, ref, style, children, ...props }: SlideProps): react.JSX.Element;
128
+ type TabsProps = ComponentProps<'div'>;
129
+ declare function CarouselTabs({ ref, ...props }: TabsProps): react.JSX.Element;
130
+ interface TabProps extends ComponentProps<'button'> {
131
+ /** The index of the slide this tab shows. */
132
+ index: number;
133
+ }
134
+ declare function CarouselTab({ index, ref, style, onClick, ...props }: TabProps): react.JSX.Element;
135
+ type PrevProps = ComponentProps<'button'>;
136
+ type NextProps = ComponentProps<'button'>;
137
+ /**
138
+ * Stays focusable at the first slide (with `aria-disabled`) so keyboard focus is
139
+ * not dropped to the page when it stops being useful.
140
+ */
141
+ declare function CarouselPrev({ onClick, children, ...props }: PrevProps): react.JSX.Element;
142
+ declare function CarouselNext({ onClick, children, ...props }: NextProps): react.JSX.Element;
143
+
144
+ type parts_NextProps = NextProps;
145
+ type parts_PrevProps = PrevProps;
146
+ type parts_RootProps = RootProps;
147
+ type parts_SlideProps = SlideProps;
148
+ type parts_TabProps = TabProps;
149
+ type parts_TabsProps = TabsProps;
150
+ type parts_TrackProps = TrackProps;
151
+ type parts_ViewportProps = ViewportProps;
152
+ declare namespace parts {
153
+ export { CarouselNext as Next, CarouselPrev as Prev, CarouselRoot as Root, CarouselSlide as Slide, CarouselTab as Tab, CarouselTabs as Tabs, CarouselTrack as Track, CarouselViewport as Viewport };
154
+ export type { parts_NextProps as NextProps, parts_PrevProps as PrevProps, parts_RootProps as RootProps, parts_SlideProps as SlideProps, parts_TabProps as TabProps, parts_TabsProps as TabsProps, parts_TrackProps as TrackProps, parts_ViewportProps as ViewportProps };
155
+ }
156
+
157
+ export { parts as Carousel, presence, useCarousel, useCarouselProgress, useSlide };
158
+ export type { CarouselApi, CarouselProgress, KeyboardMode, NavigateOptions, NextProps, PerceptualSpring, PhysicalSpring, PrevProps, ProgressListener, ReducedMotionMode, RootProps, SlideContextValue, SlideProps, SpringOptions, TabProps, TabsProps, TrackProps, ViewportProps };
@@ -0,0 +1,158 @@
1
+ import * as react from 'react';
2
+ import { ComponentProps } from 'react';
3
+
4
+ type ProgressListener = (progress: number) => void;
5
+ /**
6
+ * The carousel's continuous position, in slides: 0 is the first slide, 1.5 is
7
+ * halfway between the second and third. It changes every animation frame without
8
+ * re-rendering React; read it with `get()` or listen with `subscribe()`.
9
+ */
10
+ interface CarouselProgress {
11
+ get(): number;
12
+ /** Slides per second. */
13
+ getVelocity(): number;
14
+ /** Calls `listener` immediately with the current value, then on every change. */
15
+ subscribe(listener: ProgressListener): () => void;
16
+ }
17
+
18
+ /**
19
+ * Physical spring parameters. Defaults reproduce the original Kami carousel.
20
+ */
21
+ interface PhysicalSpring {
22
+ stiffness?: number;
23
+ damping?: number;
24
+ mass?: number;
25
+ duration?: never;
26
+ bounce?: never;
27
+ }
28
+ /**
29
+ * Perceptual spring parameters, as in SwiftUI and Motion's `visualDuration`.
30
+ * `duration` is in seconds; `bounce` runs from -1 (overdamped) through 0 (critically
31
+ * damped) to 1 (undamped).
32
+ */
33
+ interface PerceptualSpring {
34
+ duration: number;
35
+ bounce?: number;
36
+ stiffness?: never;
37
+ damping?: never;
38
+ mass?: never;
39
+ }
40
+ type SpringOptions = PhysicalSpring | PerceptualSpring;
41
+
42
+ type KeyboardMode = 'focus' | 'global' | false;
43
+ type ReducedMotionMode = 'user' | 'always' | 'never';
44
+ interface NavigateOptions {
45
+ /** Set to false to move instantly. Defaults to true. */
46
+ animate?: boolean;
47
+ /** Starting velocity in slides per second. Defaults to the current velocity. */
48
+ velocity?: number;
49
+ }
50
+
51
+ interface SlideContextValue {
52
+ index: number;
53
+ isActive: boolean;
54
+ }
55
+
56
+ interface CarouselApi {
57
+ /** The slide the carousel is on, or heading to. */
58
+ index: number;
59
+ /** How many slides are mounted. 0 until the first client render. */
60
+ count: number;
61
+ /** True while a pointer is dragging the slides. */
62
+ isDragging: boolean;
63
+ canPrev: boolean;
64
+ canNext: boolean;
65
+ goTo: (index: number, options?: NavigateOptions) => void;
66
+ next: (options?: NavigateOptions) => void;
67
+ prev: (options?: NavigateOptions) => void;
68
+ /** The continuous position, updated every frame without re-rendering. */
69
+ progress: CarouselProgress;
70
+ }
71
+ /**
72
+ * Reads the carousel's state and controls from any component inside
73
+ * `<Carousel.Root>`. Re-renders when the index, count or drag state changes, never
74
+ * per frame; use `useCarouselProgress` for per-frame work.
75
+ */
76
+ declare function useCarousel(): CarouselApi;
77
+ /**
78
+ * Calls `listener` with the carousel's position on every frame it moves, and once
79
+ * on mount. Use it to drive styles imperatively (captions, parallax, progress bars)
80
+ * without re-rendering. The listener always sees the latest props and state.
81
+ */
82
+ declare function useCarouselProgress(listener: ProgressListener): void;
83
+ /** Reads the enclosing `<Carousel.Slide>`'s index and whether it is the current slide. */
84
+ declare function useSlide(): SlideContextValue;
85
+
86
+ /** How present an item is at a given progress: 1 when it is the current slide, 0 a slide or more away. */
87
+ declare function presence(progress: number, index: number): number;
88
+
89
+ interface RootProps extends ComponentProps<'section'> {
90
+ /** The current slide, for a controlled carousel. Pair with `onIndexChange`. */
91
+ index?: number;
92
+ /** The slide to start on, for an uncontrolled carousel. Defaults to 0. */
93
+ defaultIndex?: number;
94
+ /** Called when a gesture, key, tab or button asks for a new slide. */
95
+ onIndexChange?: (index: number) => void;
96
+ /**
97
+ * The spring that moves the carousel: `{ stiffness, damping, mass }` or
98
+ * `{ duration, bounce }` with `duration` in seconds.
99
+ * Defaults to `{ stiffness: 90, damping: 18.1, mass: 0.9 }`.
100
+ */
101
+ spring?: SpringOptions;
102
+ /**
103
+ * Where arrow keys move the carousel: `"focus"` when focus is inside it, `"global"`
104
+ * anywhere on the page outside text fields and other arrow-key widgets, or `false`.
105
+ * Defaults to `"focus"`.
106
+ */
107
+ keyboard?: KeyboardMode;
108
+ /** Lets horizontal trackpad swipes page through slides. Defaults to true. */
109
+ wheel?: boolean;
110
+ /** Lets pointers drag the slides. Defaults to true. */
111
+ draggable?: boolean;
112
+ /**
113
+ * `"user"` follows `prefers-reduced-motion`, `"always"` moves instantly and
114
+ * `"never"` always animates. Defaults to `"user"`.
115
+ */
116
+ reducedMotion?: ReducedMotionMode;
117
+ }
118
+ declare function CarouselRoot({ index: indexProp, defaultIndex, onIndexChange, spring, keyboard, wheel, draggable, reducedMotion, ref, style, children, ...props }: RootProps): react.JSX.Element;
119
+ type ViewportProps = ComponentProps<'div'>;
120
+ declare function CarouselViewport({ ref, ...props }: ViewportProps): react.JSX.Element;
121
+ type TrackProps = ComponentProps<'div'>;
122
+ declare function CarouselTrack({ ref, ...props }: TrackProps): react.JSX.Element;
123
+ interface SlideProps extends ComponentProps<'div'> {
124
+ /** This slide's position, from 0. Indexes must run 0, 1, 2… without gaps. */
125
+ index: number;
126
+ }
127
+ declare function CarouselSlide({ index, ref, style, children, ...props }: SlideProps): react.JSX.Element;
128
+ type TabsProps = ComponentProps<'div'>;
129
+ declare function CarouselTabs({ ref, ...props }: TabsProps): react.JSX.Element;
130
+ interface TabProps extends ComponentProps<'button'> {
131
+ /** The index of the slide this tab shows. */
132
+ index: number;
133
+ }
134
+ declare function CarouselTab({ index, ref, style, onClick, ...props }: TabProps): react.JSX.Element;
135
+ type PrevProps = ComponentProps<'button'>;
136
+ type NextProps = ComponentProps<'button'>;
137
+ /**
138
+ * Stays focusable at the first slide (with `aria-disabled`) so keyboard focus is
139
+ * not dropped to the page when it stops being useful.
140
+ */
141
+ declare function CarouselPrev({ onClick, children, ...props }: PrevProps): react.JSX.Element;
142
+ declare function CarouselNext({ onClick, children, ...props }: NextProps): react.JSX.Element;
143
+
144
+ type parts_NextProps = NextProps;
145
+ type parts_PrevProps = PrevProps;
146
+ type parts_RootProps = RootProps;
147
+ type parts_SlideProps = SlideProps;
148
+ type parts_TabProps = TabProps;
149
+ type parts_TabsProps = TabsProps;
150
+ type parts_TrackProps = TrackProps;
151
+ type parts_ViewportProps = ViewportProps;
152
+ declare namespace parts {
153
+ export { CarouselNext as Next, CarouselPrev as Prev, CarouselRoot as Root, CarouselSlide as Slide, CarouselTab as Tab, CarouselTabs as Tabs, CarouselTrack as Track, CarouselViewport as Viewport };
154
+ export type { parts_NextProps as NextProps, parts_PrevProps as PrevProps, parts_RootProps as RootProps, parts_SlideProps as SlideProps, parts_TabProps as TabProps, parts_TabsProps as TabsProps, parts_TrackProps as TrackProps, parts_ViewportProps as ViewportProps };
155
+ }
156
+
157
+ export { parts as Carousel, presence, useCarousel, useCarouselProgress, useSlide };
158
+ export type { CarouselApi, CarouselProgress, KeyboardMode, NavigateOptions, NextProps, PerceptualSpring, PhysicalSpring, PrevProps, ProgressListener, ReducedMotionMode, RootProps, SlideContextValue, SlideProps, SpringOptions, TabProps, TabsProps, TrackProps, ViewportProps };