@krate/motion 0.0.0-stage → 0.5.0-rc.18

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,25 @@
1
+ // The component types of `motion.<tag>` in Krate pages.
2
+
3
+ import type { MotionProps, MotionStyle } from './types.js';
4
+
5
+ type Intrinsic = JSX.IntrinsicElements;
6
+
7
+ /** The props of `motion.<Tag>`: the element's attributes plus Motion props. */
8
+ export type HTMLMotionProps<Tag extends keyof Intrinsic> = Omit<Intrinsic[Tag], 'style' | keyof MotionProps> &
9
+ MotionProps & {
10
+ style?: MotionStyle | string;
11
+ ref?: unknown;
12
+ key?: string | number;
13
+ children?: any;
14
+ };
15
+
16
+ export type SVGMotionProps<Tag extends keyof Intrinsic> = HTMLMotionProps<Tag>;
17
+
18
+ export type MotionComponent<P> = (props: P & MotionProps & { ref?: unknown; key?: string | number }) => any;
19
+
20
+ export type MotionNamespace = { [Tag in keyof Intrinsic]: (props: HTMLMotionProps<Tag>) => any } & {
21
+ /** A motion version of a component that passes `ref` and `style` to its element. */
22
+ create<P>(component: ((props: P) => any) | string, options?: { forwardMotionProps?: boolean }): MotionComponent<P>;
23
+ /** @deprecated use motion.create */
24
+ <P>(component: ((props: P) => any) | string): MotionComponent<P>;
25
+ };
@@ -0,0 +1,112 @@
1
+ // Reorder.Group and Reorder.Item for Krate pages: a list whose items are
2
+ // reordered by dragging. The elements are compiled like motion elements
3
+ // (this module writes what the compiler writes for <motion.ul>).
4
+
5
+ import { createContext, untrack } from '@krate/runtime';
6
+ import { __kmRef, isMotionValue, motionValue, transformMotionValue,createReorderGroup,type ReorderContext } from '@krate/motion/engine';
7
+ import { dragFeatures } from '@krate/motion/engine/drag';
8
+ import { layoutFeatures } from '@krate/motion/engine/layout';
9
+ import { gestureFeatures } from '@krate/motion/engine/gestures';
10
+ import { MotionScope } from './context.js';
11
+ import type { MotionProps } from './types.js';
12
+
13
+ const ReorderCtx = createContext<ReorderContext<any> | null>(null);
14
+
15
+ type Attrs = {
16
+ as?: string;
17
+ class?: string;
18
+ className?: string;
19
+ id?: string;
20
+ role?: string;
21
+ title?: string;
22
+ tabIndex?: number;
23
+ 'aria-label'?: string;
24
+ 'data-testid'?: string;
25
+ ref?: unknown;
26
+ key?: string | number;
27
+ children?: any;
28
+ };
29
+
30
+ export interface ReorderGroupProps<T> extends Omit<MotionProps, 'values'>, Attrs {
31
+ axis?: 'x' | 'y';
32
+ values: T[];
33
+ onReorder: (values: T[]) => void;
34
+ }
35
+
36
+ export interface ReorderItemProps<T> extends Omit<MotionProps, 'values'>, Attrs {
37
+ value: T;
38
+ }
39
+
40
+ function motionPropsOf(props: Record<string, any>): Record<string, any> {
41
+ const out: Record<string, any> = {};
42
+ for (const key of ['initial', 'animate', 'exit', 'transition', 'variants', 'custom', 'style', 'whileHover', 'whileTap', 'whileDrag', 'whileFocus', 'whileInView', 'onAnimationStart', 'onAnimationComplete', 'onDragStart', 'onDragEnd', 'onLayoutAnimationStart', 'onLayoutAnimationComplete', 'dragTransition', 'dragElastic', 'dragMomentum', 'dragListener', 'dragControls', 'dragConstraints']) {
43
+ if (props[key] !== undefined) out[key] = props[key];
44
+ }
45
+ return out;
46
+ }
47
+
48
+ /** A list (`as`, default "ul") whose items are reordered by dragging them along `axis`. */
49
+ export function Group<T>(props: ReorderGroupProps<T>) {
50
+ const scope = MotionScope.useContext();
51
+ const ctx = createReorderGroup(() => props as any);
52
+ const Tag = (props.as ?? 'ul') as 'ul';
53
+ return (
54
+ <Tag
55
+ class={props.class ?? props.className}
56
+ id={props.id}
57
+ role={props.role}
58
+ title={props.title}
59
+ tabIndex={props.tabIndex}
60
+ aria-label={props['aria-label']}
61
+ data-testid={props['data-testid']}
62
+ ref={__kmRef(() => motionPropsOf(props as any), props.ref, scope, props.as ?? 'ul', [gestureFeatures])}
63
+ >
64
+ <ReorderCtx.Provider value={ctx}>{props.children}</ReorderCtx.Provider>
65
+ </Tag>
66
+ );
67
+ }
68
+
69
+ /** An item of a Reorder.Group (`as`, default "li"), dragged along the group's axis. */
70
+ export function Item<T>(props: ReorderItemProps<T>) {
71
+ const scope = MotionScope.useContext();
72
+ const group = ReorderCtx.useContext();
73
+ const Tag = (props.as ?? 'li') as 'li';
74
+ const style: any = props.style;
75
+ const x = style && isMotionValue(style.x) ? style.x : motionValue(0);
76
+ const y = style && isMotionValue(style.y) ? style.y : motionValue(0);
77
+ const zIndex = transformMotionValue([x, y], ([lx, ly]: number[]) => (lx || ly ? 1 : 'unset'));
78
+ const getProps = (): Record<string, any> => {
79
+ const g = group && (typeof group === 'function' ? (group as any)() : group);
80
+ const axis = g ? g.axis : 'y';
81
+ return {
82
+ ...motionPropsOf(props as any),
83
+ drag: axis,
84
+ dragSnapToOrigin: true,
85
+ layout: true,
86
+ style: { ...(typeof style === 'object' ? style : {}), x, y, zIndex },
87
+ onDrag: (event: PointerEvent, info: any) => {
88
+ const { velocity } = info;
89
+ if (velocity[axis] && g) g.updateOrder(props.value, axis === 'x' ? x.get() : y.get(), velocity[axis]);
90
+ (props as any).onDrag?.(event, info);
91
+ },
92
+ onLayoutMeasure: (box: any) => {
93
+ if (g) g.registerItem(props.value, box);
94
+ },
95
+ };
96
+ };
97
+ return (
98
+ <Tag
99
+ class={props.class ?? props.className}
100
+ id={props.id}
101
+ role={props.role}
102
+ title={props.title}
103
+ tabIndex={props.tabIndex}
104
+ aria-label={props['aria-label']}
105
+ data-testid={props['data-testid']}
106
+ ref={__kmRef(getProps, props.ref, scope, props.as ?? 'li', [layoutFeatures, dragFeatures, gestureFeatures])}
107
+ style={untrack(() => (typeof style === 'string' ? style : undefined))}
108
+ >
109
+ {props.children}
110
+ </Tag>
111
+ );
112
+ }
package/src/types.ts ADDED
@@ -0,0 +1,179 @@
1
+ // Types of Motion props shared by the Krate and the Preact builds, after
2
+ // motion/react's.
3
+
4
+ import type { AnimationPlaybackControls, MotionValue } from './engine/motion-value.js';
5
+ import type { Transition } from './engine/animation.js';
6
+ import type { DragControls, PanInfo, Point } from './engine/drag.js';
7
+ import type { EventInfo } from './engine/gestures.js';
8
+ import type { LegacyAnimationControls } from './engine/animate.js';
9
+
10
+ export type { Transition, MotionValue, AnimationPlaybackControls, PanInfo, Point, EventInfo, DragControls, LegacyAnimationControls };
11
+
12
+ type StyleValue = string | number | undefined | null;
13
+
14
+ /** Transform shorthands and other values Motion animates on HTML and SVG elements. */
15
+ export interface TransformProperties {
16
+ x?: StyleValue;
17
+ y?: StyleValue;
18
+ z?: StyleValue;
19
+ translateX?: StyleValue;
20
+ translateY?: StyleValue;
21
+ translateZ?: StyleValue;
22
+ rotate?: StyleValue;
23
+ rotateX?: StyleValue;
24
+ rotateY?: StyleValue;
25
+ rotateZ?: StyleValue;
26
+ scale?: StyleValue;
27
+ scaleX?: StyleValue;
28
+ scaleY?: StyleValue;
29
+ scaleZ?: StyleValue;
30
+ skew?: StyleValue;
31
+ skewX?: StyleValue;
32
+ skewY?: StyleValue;
33
+ originX?: StyleValue;
34
+ originY?: StyleValue;
35
+ originZ?: StyleValue;
36
+ perspective?: StyleValue;
37
+ transformPerspective?: StyleValue;
38
+ }
39
+
40
+ export interface SVGPathProperties {
41
+ pathLength?: number;
42
+ pathOffset?: number;
43
+ pathSpacing?: number;
44
+ }
45
+
46
+ type CSSValues = { [key: string]: StyleValue | StyleValue[] | Array<StyleValue | null> | ((i: number, total: number) => StyleValue) | object | undefined };
47
+
48
+ /** A target: values (or keyframe arrays) plus an optional transition. */
49
+ export type TargetAndTransition = TransformProperties &
50
+ SVGPathProperties &
51
+ CSSValues & {
52
+ transition?: Transition;
53
+ transitionEnd?: Record<string, StyleValue>;
54
+ };
55
+
56
+ export type VariantLabels = string | string[];
57
+
58
+ export type Variant = TargetAndTransition | ((custom: any, current: Record<string, any>, velocity: Record<string, any>) => TargetAndTransition | string);
59
+
60
+ export type Variants = { [key: string]: Variant };
61
+
62
+ /** A style prop: CSS values, transform shorthands and motion values. */
63
+ export type MotionStyle = { [key: string]: StyleValue | MotionValue<any> } & {
64
+ [K in keyof TransformProperties]?: StyleValue | MotionValue<any>;
65
+ };
66
+
67
+ export interface ViewportOptions {
68
+ root?: { current: Element | null } | Element;
69
+ once?: boolean;
70
+ margin?: string;
71
+ amount?: 'some' | 'all' | number;
72
+ }
73
+
74
+ export interface DragTransition extends Transition {}
75
+
76
+ export interface BoundingBox {
77
+ top?: number;
78
+ left?: number;
79
+ right?: number;
80
+ bottom?: number;
81
+ }
82
+
83
+ /** The props every motion element takes besides its own attributes. */
84
+ export interface MotionProps {
85
+ initial?: boolean | TargetAndTransition | VariantLabels;
86
+ animate?: TargetAndTransition | VariantLabels | LegacyAnimationControls | boolean;
87
+ exit?: TargetAndTransition | VariantLabels;
88
+ transition?: Transition;
89
+ variants?: Variants;
90
+ custom?: any;
91
+ inherit?: boolean;
92
+ style?: MotionStyle | string;
93
+ values?: Record<string, MotionValue>;
94
+ transformTemplate?: (transform: Record<string, string>, generated: string) => string;
95
+ onUpdate?: (latest: Record<string, any>) => void;
96
+ onAnimationStart?: (definition: any) => void;
97
+ onAnimationComplete?: (definition: any) => void;
98
+ whileHover?: TargetAndTransition | VariantLabels;
99
+ whileTap?: TargetAndTransition | VariantLabels;
100
+ whileFocus?: TargetAndTransition | VariantLabels;
101
+ whileDrag?: TargetAndTransition | VariantLabels;
102
+ whileInView?: TargetAndTransition | VariantLabels;
103
+ viewport?: ViewportOptions;
104
+ onViewportEnter?: (entry: IntersectionObserverEntry | null) => void;
105
+ onViewportLeave?: (entry: IntersectionObserverEntry | null) => void;
106
+ onHoverStart?: (event: PointerEvent, info: EventInfo) => void;
107
+ onHoverEnd?: (event: PointerEvent, info: EventInfo) => void;
108
+ onTap?: (event: PointerEvent, info: EventInfo) => void;
109
+ onTapStart?: (event: PointerEvent, info: EventInfo) => void;
110
+ onTapCancel?: (event: PointerEvent, info: EventInfo) => void;
111
+ globalTapTarget?: boolean;
112
+ onPan?: (event: PointerEvent, info: PanInfo) => void;
113
+ onPanStart?: (event: PointerEvent, info: PanInfo) => void;
114
+ onPanEnd?: (event: PointerEvent, info: PanInfo) => void;
115
+ onPanSessionStart?: (event: PointerEvent, info: PanInfo) => void;
116
+ drag?: boolean | 'x' | 'y';
117
+ dragConstraints?: false | BoundingBox | { current: Element | null };
118
+ dragElastic?: boolean | number | BoundingBox;
119
+ dragMomentum?: boolean;
120
+ dragTransition?: DragTransition;
121
+ dragSnapToOrigin?: boolean;
122
+ dragDirectionLock?: boolean;
123
+ dragListener?: boolean;
124
+ dragControls?: DragControls;
125
+ dragPropagation?: boolean;
126
+ onDrag?: (event: PointerEvent, info: PanInfo) => void;
127
+ onDragStart?: (event: PointerEvent, info: PanInfo) => void;
128
+ onDragEnd?: (event: PointerEvent, info: PanInfo) => void;
129
+ onDirectionLock?: (axis: 'x' | 'y') => void;
130
+ onDragTransitionEnd?: () => void;
131
+ onMeasureDragConstraints?: (constraints: BoundingBox) => BoundingBox | void;
132
+ layout?: boolean | 'position' | 'size' | 'preserve-aspect';
133
+ layoutId?: string;
134
+ layoutDependency?: any;
135
+ layoutScroll?: boolean;
136
+ layoutRoot?: boolean;
137
+ onLayoutAnimationStart?: () => void;
138
+ onLayoutAnimationComplete?: () => void;
139
+ onLayoutMeasure?: (layout: any, prevLayout?: any) => void;
140
+ onBeforeLayoutMeasure?: () => void;
141
+ }
142
+
143
+ export interface AnimatePresenceProps {
144
+ children?: any;
145
+ initial?: boolean;
146
+ custom?: any;
147
+ onExitComplete?: () => void;
148
+ mode?: 'sync' | 'wait' | 'popLayout';
149
+ propagate?: boolean;
150
+ anchorX?: 'left' | 'right';
151
+ root?: unknown;
152
+ presenceAffectsLayout?: boolean;
153
+ }
154
+
155
+ export interface MotionConfigProps {
156
+ children?: any;
157
+ transition?: Transition;
158
+ reducedMotion?: 'user' | 'always' | 'never';
159
+ nonce?: string;
160
+ isStatic?: boolean;
161
+ transformPagePoint?: (point: Point) => Point;
162
+ }
163
+
164
+ export interface LayoutGroupProps {
165
+ children?: any;
166
+ id?: string;
167
+ inherit?: boolean | 'id';
168
+ }
169
+
170
+ export interface FeatureBundle {
171
+ readonly __features: string[];
172
+ renderer?: unknown;
173
+ }
174
+
175
+ export interface LazyMotionProps {
176
+ children?: any;
177
+ features: FeatureBundle | (() => Promise<FeatureBundle | { default: FeatureBundle }>);
178
+ strict?: boolean;
179
+ }