@rio-m3e/core 0.0.0-stage → 0.1.0
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/LICENSE +202 -0
- package/NOTICE +6 -0
- package/README.md +6 -2
- package/dist/index.d.ts +268 -0
- package/dist/index.js +605 -0
- package/package.json +34 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @rio-m3e/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Framework-agnostic logic for [Rio](https://github.com/LegenLee/rio), a Material 3 Expressive implementation: component prop types, defaults, state, and accessibility attributes, plus the DOM behaviors components share (ripple, shape morph, springs, and drag).
|
|
4
|
+
|
|
5
|
+
Framework adapters such as [`@rio-m3e/vue`](https://www.npmjs.com/package/@rio-m3e/vue) are built on it, and install it for you. Install it directly only to build components of your own.
|
|
6
|
+
|
|
7
|
+
Licensed under the Apache License 2.0.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
export declare const BUTTON_VARIANTS: readonly ["filled", "tonal", "outlined", "elevated", "text"];
|
|
2
|
+
export type ButtonVariant = (typeof BUTTON_VARIANTS)[number];
|
|
3
|
+
/** M3E button sizes. `small` is the default. */
|
|
4
|
+
export declare const BUTTON_SIZES: readonly ["xsmall", "small", "medium", "large", "xlarge"];
|
|
5
|
+
export type ButtonSize = (typeof BUTTON_SIZES)[number];
|
|
6
|
+
export type ButtonShape = 'round' | 'square';
|
|
7
|
+
export interface ButtonProps {
|
|
8
|
+
/** Defaults to `'filled'`. */
|
|
9
|
+
variant?: ButtonVariant;
|
|
10
|
+
/** Defaults to `'small'`. */
|
|
11
|
+
size?: ButtonSize;
|
|
12
|
+
/** Defaults to `'round'`. */
|
|
13
|
+
shape?: ButtonShape;
|
|
14
|
+
/**
|
|
15
|
+
* Makes it a toggle button, which reports `selected` changes. The spec has no toggle for text
|
|
16
|
+
* buttons, so `toggle` is ignored on them.
|
|
17
|
+
*/
|
|
18
|
+
toggle?: boolean;
|
|
19
|
+
/** Whether a toggle button is on. */
|
|
20
|
+
selected?: boolean;
|
|
21
|
+
/** Native disabled: not focusable and not pressable. This is the spec's behavior. */
|
|
22
|
+
disabled?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Disabled but still focusable, so it stays discoverable, for example in a toolbar. Clicks are
|
|
25
|
+
* blocked in JavaScript instead. This deviates from the spec, which says disabled components
|
|
26
|
+
* can't be focused.
|
|
27
|
+
*/
|
|
28
|
+
softDisabled?: boolean;
|
|
29
|
+
/** Defaults to `'button'`, so a button inside a form doesn't submit it by accident. */
|
|
30
|
+
type?: 'button' | 'submit' | 'reset';
|
|
31
|
+
}
|
|
32
|
+
export declare const buttonDefaults: {
|
|
33
|
+
variant: "filled";
|
|
34
|
+
size: "small";
|
|
35
|
+
shape: "round";
|
|
36
|
+
toggle: false;
|
|
37
|
+
selected: false;
|
|
38
|
+
disabled: false;
|
|
39
|
+
softDisabled: false;
|
|
40
|
+
type: "button";
|
|
41
|
+
};
|
|
42
|
+
export interface ButtonOptions {
|
|
43
|
+
/** Returns the current prop values. The adapter owns the state. */
|
|
44
|
+
props: () => ButtonProps;
|
|
45
|
+
/** Called for a click that isn't blocked. */
|
|
46
|
+
onClick: (event: MouseEvent) => void;
|
|
47
|
+
/** Called after an unblocked click on a toggle button, with the new value. */
|
|
48
|
+
onSelectedChange: (selected: boolean) => void;
|
|
49
|
+
}
|
|
50
|
+
export interface ButtonApi {
|
|
51
|
+
rootAttrs: () => Record<string, string | undefined>;
|
|
52
|
+
/** Attach to the root element's `click`. */
|
|
53
|
+
handleClick: (event: MouseEvent) => void;
|
|
54
|
+
}
|
|
55
|
+
export declare function createButton(options: ButtonOptions): ButtonApi;
|
|
56
|
+
export declare const ICON_BUTTON_VARIANTS: readonly ["filled", "tonal", "outlined", "standard"];
|
|
57
|
+
export type IconButtonVariant = (typeof ICON_BUTTON_VARIANTS)[number];
|
|
58
|
+
/** M3E icon button widths. `default` makes a square (or circle) as wide as it is tall. */
|
|
59
|
+
export declare const ICON_BUTTON_WIDTHS: readonly ["narrow", "default", "wide"];
|
|
60
|
+
export type IconButtonWidth = (typeof ICON_BUTTON_WIDTHS)[number];
|
|
61
|
+
export interface IconButtonProps {
|
|
62
|
+
/**
|
|
63
|
+
* The accessible name. It should describe the action, such as "Add to favorites", not the icon,
|
|
64
|
+
* such as "Heart".
|
|
65
|
+
*/
|
|
66
|
+
label: string;
|
|
67
|
+
/** Defaults to `'filled'`, the spec's default. */
|
|
68
|
+
variant?: IconButtonVariant;
|
|
69
|
+
/** Defaults to `'small'`. */
|
|
70
|
+
size?: ButtonSize;
|
|
71
|
+
/** Defaults to `'default'`. */
|
|
72
|
+
width?: IconButtonWidth;
|
|
73
|
+
/** Defaults to `'round'`. */
|
|
74
|
+
shape?: ButtonShape;
|
|
75
|
+
/** Makes it a toggle button, which reports `selected` changes. All four variants can toggle. */
|
|
76
|
+
toggle?: boolean;
|
|
77
|
+
/** Whether a toggle button is on. */
|
|
78
|
+
selected?: boolean;
|
|
79
|
+
/** Native disabled: not focusable and not pressable. This is the spec's behavior. */
|
|
80
|
+
disabled?: boolean;
|
|
81
|
+
/** Disabled but still focusable, as on the button. Deviates from the spec. */
|
|
82
|
+
softDisabled?: boolean;
|
|
83
|
+
/** Defaults to `'button'`, so it doesn't submit a form by accident. */
|
|
84
|
+
type?: 'button' | 'submit' | 'reset';
|
|
85
|
+
}
|
|
86
|
+
export declare const iconButtonDefaults: {
|
|
87
|
+
variant: "filled";
|
|
88
|
+
size: "small";
|
|
89
|
+
width: "default";
|
|
90
|
+
shape: "round";
|
|
91
|
+
toggle: false;
|
|
92
|
+
selected: false;
|
|
93
|
+
disabled: false;
|
|
94
|
+
softDisabled: false;
|
|
95
|
+
type: "button";
|
|
96
|
+
};
|
|
97
|
+
export interface IconButtonOptions {
|
|
98
|
+
/** Returns the current prop values. The adapter owns the state. */
|
|
99
|
+
props: () => IconButtonProps;
|
|
100
|
+
/** Called for a click that isn't blocked. */
|
|
101
|
+
onClick: (event: MouseEvent) => void;
|
|
102
|
+
/** Called after an unblocked click on a toggle button, with the new value. */
|
|
103
|
+
onSelectedChange: (selected: boolean) => void;
|
|
104
|
+
}
|
|
105
|
+
export declare function createIconButton(options: IconButtonOptions): ButtonApi;
|
|
106
|
+
export interface DragInfo {
|
|
107
|
+
/** Distance from where the press started, in px. */
|
|
108
|
+
dx: number;
|
|
109
|
+
dy: number;
|
|
110
|
+
/** Over the last moments of the drag, in px per second. */
|
|
111
|
+
velocityX: number;
|
|
112
|
+
velocityY: number;
|
|
113
|
+
}
|
|
114
|
+
export interface DragOptions {
|
|
115
|
+
/** Pointer types that can drag. Defaults to touch and pen, as M3 specifies for the switch. */
|
|
116
|
+
pointerTypes?: readonly string[];
|
|
117
|
+
/** Distance a pointer must move before a drag starts. Defaults to 8px. */
|
|
118
|
+
threshold?: number;
|
|
119
|
+
onStart?: () => void;
|
|
120
|
+
onMove: (info: DragInfo) => void;
|
|
121
|
+
onEnd: (info: DragInfo) => void;
|
|
122
|
+
/** The browser took the pointer, for example to scroll the page. Defaults to `onEnd`. */
|
|
123
|
+
onCancel?: (info: DragInfo) => void;
|
|
124
|
+
}
|
|
125
|
+
export interface DragHandle {
|
|
126
|
+
detach(): void;
|
|
127
|
+
}
|
|
128
|
+
export declare function attachDrag(element: HTMLElement, options: DragOptions): DragHandle;
|
|
129
|
+
export interface RippleHandle {
|
|
130
|
+
/** Removes the listeners and the `data-ripple` marker. */
|
|
131
|
+
detach(): void;
|
|
132
|
+
}
|
|
133
|
+
export interface RippleOptions {
|
|
134
|
+
/**
|
|
135
|
+
* The element that draws the ripple and sets its size, when it isn't the element that receives
|
|
136
|
+
* presses. A switch, for example, takes presses on its whole track but ripples in the circle
|
|
137
|
+
* around its handle. Defaults to the element itself.
|
|
138
|
+
*/
|
|
139
|
+
surface?: HTMLElement;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Adds a press ripple to an element. While pressed, the element (and its surface) has
|
|
143
|
+
* `data-pressed`. Load `@rio-m3e/styles/base/ripple.css` for the visuals; it styles elements
|
|
144
|
+
* marked `data-ripple`.
|
|
145
|
+
*/
|
|
146
|
+
export declare function attachRipple(element: HTMLElement, options?: RippleOptions): RippleHandle;
|
|
147
|
+
export interface ShapeMorphOptions {
|
|
148
|
+
/** The motion token whose spring drives the morph. Defaults to `'fast-spatial'`. */
|
|
149
|
+
token?: string;
|
|
150
|
+
/** Attributes whose changes can move the target. Defaults to Rio's state attributes. */
|
|
151
|
+
watch?: string[];
|
|
152
|
+
}
|
|
153
|
+
export interface ShapeMorphHandle {
|
|
154
|
+
detach(): void;
|
|
155
|
+
}
|
|
156
|
+
export declare function attachShapeMorph(element: HTMLElement, options?: ShapeMorphOptions): ShapeMorphHandle;
|
|
157
|
+
export interface SpringPropertyOptions {
|
|
158
|
+
/** A registered <length> custom property that holds the target for the current state. */
|
|
159
|
+
target: string;
|
|
160
|
+
/** Writes the animated value in px, or clears it with `undefined` at rest. */
|
|
161
|
+
apply: (value: number | undefined) => void;
|
|
162
|
+
/** The motion token whose spring drives the value. Defaults to `'fast-spatial'`. */
|
|
163
|
+
token?: string;
|
|
164
|
+
/** Attributes whose changes can move the target. Defaults to Rio's state attributes. */
|
|
165
|
+
watch?: string[];
|
|
166
|
+
}
|
|
167
|
+
export interface SpringPropertyHandle {
|
|
168
|
+
/** The current animated value. */
|
|
169
|
+
readonly value: number;
|
|
170
|
+
/** Takes manual control and sets the value at once, for example while dragging. */
|
|
171
|
+
hold(value: number): void;
|
|
172
|
+
/** Gives control back: springs from the held value to the CSS target, starting at `velocity`. */
|
|
173
|
+
release(velocity?: number): void;
|
|
174
|
+
detach(): void;
|
|
175
|
+
}
|
|
176
|
+
/** The data attributes Rio's components use for state. */
|
|
177
|
+
export declare const STATE_ATTRIBUTES: string[];
|
|
178
|
+
export declare function attachSpringProperty(element: HTMLElement, options: SpringPropertyOptions): SpringPropertyHandle;
|
|
179
|
+
export interface SpringParams {
|
|
180
|
+
/** 1 settles without overshoot; lower values bounce, higher values approach more slowly. */
|
|
181
|
+
dampingRatio: number;
|
|
182
|
+
stiffness: number;
|
|
183
|
+
}
|
|
184
|
+
export interface SpringState {
|
|
185
|
+
value: number;
|
|
186
|
+
/** Units per second. */
|
|
187
|
+
velocity: number;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Where a spring is `t` seconds after it starts at `from` with `velocity`, heading to `to`.
|
|
191
|
+
*/
|
|
192
|
+
export declare function solveSpring({ dampingRatio: zeta, stiffness }: SpringParams, from: number, to: number, velocity: number, t: number): SpringState;
|
|
193
|
+
export interface SpringOptions {
|
|
194
|
+
/** Starting value. */
|
|
195
|
+
value: number;
|
|
196
|
+
/** Called on every frame while moving, and once more with the exact target at rest. */
|
|
197
|
+
onUpdate: (value: number) => void;
|
|
198
|
+
/** Called when the spring comes to rest. */
|
|
199
|
+
onRest?: () => void;
|
|
200
|
+
/**
|
|
201
|
+
* The spring is at rest when it is within this distance of the target and slower than ten times
|
|
202
|
+
* this per second. Defaults to 0.01, which suits pixel values.
|
|
203
|
+
*/
|
|
204
|
+
precision?: number;
|
|
205
|
+
}
|
|
206
|
+
export interface Spring {
|
|
207
|
+
readonly value: number;
|
|
208
|
+
readonly velocity: number;
|
|
209
|
+
readonly target: number;
|
|
210
|
+
readonly moving: boolean;
|
|
211
|
+
/**
|
|
212
|
+
* Heads to a new target from the current position. It keeps the current velocity unless one is
|
|
213
|
+
* given, for example the speed of a finger at the end of a drag.
|
|
214
|
+
*/
|
|
215
|
+
to(target: number, params: SpringParams, velocity?: number): void;
|
|
216
|
+
/** Moves to a value at once, without animating. */
|
|
217
|
+
jump(value: number): void;
|
|
218
|
+
/** Stops where it is. */
|
|
219
|
+
stop(): void;
|
|
220
|
+
}
|
|
221
|
+
/** A spring that animates a single number with `requestAnimationFrame`. */
|
|
222
|
+
export declare function createSpring(options: SpringOptions): Spring;
|
|
223
|
+
/**
|
|
224
|
+
* Reads a motion token's spring from CSS variables on an element, for example
|
|
225
|
+
* `--rio-motion-fast-spatial-damping` and `-stiffness`. Reading them from the element, rather than
|
|
226
|
+
* from the tokens package, lets a subtree that swaps motion schemes change its springs too.
|
|
227
|
+
* Returns `undefined` when the theme doesn't define them.
|
|
228
|
+
*/
|
|
229
|
+
export declare function readSpringParams(element: Element, token: string): SpringParams | undefined;
|
|
230
|
+
export interface SwitchHandleOptions {
|
|
231
|
+
/** Returns whether the switch is on. */
|
|
232
|
+
selected: () => boolean;
|
|
233
|
+
/** Called when a drag ends on the other side. */
|
|
234
|
+
onSelectedChange: (selected: boolean) => void;
|
|
235
|
+
}
|
|
236
|
+
export interface SwitchHandleHandle {
|
|
237
|
+
detach(): void;
|
|
238
|
+
}
|
|
239
|
+
export declare function attachSwitchHandle(root: HTMLElement, options: SwitchHandleOptions): SwitchHandleHandle;
|
|
240
|
+
export interface SwitchProps {
|
|
241
|
+
/** Whether the switch is on. */
|
|
242
|
+
selected?: boolean;
|
|
243
|
+
/** Native disabled: not focusable and not pressable. This is the spec's behavior. */
|
|
244
|
+
disabled?: boolean;
|
|
245
|
+
/**
|
|
246
|
+
* Disabled but still focusable, so it stays discoverable. Presses and drags are blocked in
|
|
247
|
+
* JavaScript instead. This deviates from the spec, which says disabled components can't be
|
|
248
|
+
* focused.
|
|
249
|
+
*/
|
|
250
|
+
softDisabled?: boolean;
|
|
251
|
+
}
|
|
252
|
+
export declare const switchDefaults: {
|
|
253
|
+
selected: false;
|
|
254
|
+
disabled: false;
|
|
255
|
+
softDisabled: false;
|
|
256
|
+
};
|
|
257
|
+
export interface SwitchOptions {
|
|
258
|
+
/** Returns the current prop values. The adapter owns the state. */
|
|
259
|
+
props: () => SwitchProps;
|
|
260
|
+
/** Called with the requested value. Adapters wire it to v-model, onChange, and so on. */
|
|
261
|
+
onSelectedChange: (selected: boolean) => void;
|
|
262
|
+
}
|
|
263
|
+
export interface SwitchApi {
|
|
264
|
+
rootAttrs: () => Record<string, string | undefined>;
|
|
265
|
+
/** Attach to the root element's `click`. */
|
|
266
|
+
handleClick: (event: MouseEvent) => void;
|
|
267
|
+
}
|
|
268
|
+
export declare function createSwitch(options: SwitchOptions): SwitchApi;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,605 @@
|
|
|
1
|
+
// src/button/button.ts
|
|
2
|
+
const BUTTON_VARIANTS = [
|
|
3
|
+
"filled",
|
|
4
|
+
"tonal",
|
|
5
|
+
"outlined",
|
|
6
|
+
"elevated",
|
|
7
|
+
"text"
|
|
8
|
+
];
|
|
9
|
+
/** M3E button sizes. `small` is the default. */
|
|
10
|
+
const BUTTON_SIZES = [
|
|
11
|
+
"xsmall",
|
|
12
|
+
"small",
|
|
13
|
+
"medium",
|
|
14
|
+
"large",
|
|
15
|
+
"xlarge"
|
|
16
|
+
];
|
|
17
|
+
const buttonDefaults = {
|
|
18
|
+
variant: "filled",
|
|
19
|
+
size: "small",
|
|
20
|
+
shape: "round",
|
|
21
|
+
toggle: false,
|
|
22
|
+
selected: false,
|
|
23
|
+
disabled: false,
|
|
24
|
+
softDisabled: false,
|
|
25
|
+
type: "button"
|
|
26
|
+
};
|
|
27
|
+
const isToggle = (p) => !!p.toggle && p.variant !== "text";
|
|
28
|
+
/** Defaults under the given props, skipping props that are explicitly `undefined`. */
|
|
29
|
+
const resolve = (props) => ({
|
|
30
|
+
...buttonDefaults,
|
|
31
|
+
...Object.fromEntries(Object.entries(props).filter(([, v]) => v !== void 0))
|
|
32
|
+
});
|
|
33
|
+
function createButton(options) {
|
|
34
|
+
return {
|
|
35
|
+
rootAttrs() {
|
|
36
|
+
const p = resolve(options.props());
|
|
37
|
+
const toggle = isToggle(p);
|
|
38
|
+
return {
|
|
39
|
+
type: p.type,
|
|
40
|
+
disabled: p.disabled ? "" : void 0,
|
|
41
|
+
"aria-disabled": p.softDisabled && !p.disabled ? "true" : void 0,
|
|
42
|
+
"aria-pressed": toggle ? String(p.selected) : void 0,
|
|
43
|
+
"data-variant": p.variant,
|
|
44
|
+
"data-size": p.size,
|
|
45
|
+
"data-shape": p.shape,
|
|
46
|
+
"data-selected": toggle ? String(p.selected) : void 0
|
|
47
|
+
};
|
|
48
|
+
},
|
|
49
|
+
handleClick(event) {
|
|
50
|
+
const p = options.props();
|
|
51
|
+
if (p.disabled || p.softDisabled) {
|
|
52
|
+
event.preventDefault();
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
options.onClick(event);
|
|
56
|
+
if (isToggle(p)) options.onSelectedChange(!p.selected);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
// src/icon-button/icon-button.ts
|
|
61
|
+
const ICON_BUTTON_VARIANTS = [
|
|
62
|
+
"filled",
|
|
63
|
+
"tonal",
|
|
64
|
+
"outlined",
|
|
65
|
+
"standard"
|
|
66
|
+
];
|
|
67
|
+
/** M3E icon button widths. `default` makes a square (or circle) as wide as it is tall. */
|
|
68
|
+
const ICON_BUTTON_WIDTHS = [
|
|
69
|
+
"narrow",
|
|
70
|
+
"default",
|
|
71
|
+
"wide"
|
|
72
|
+
];
|
|
73
|
+
const iconButtonDefaults = {
|
|
74
|
+
variant: "filled",
|
|
75
|
+
size: "small",
|
|
76
|
+
width: "default",
|
|
77
|
+
shape: "round",
|
|
78
|
+
toggle: false,
|
|
79
|
+
selected: false,
|
|
80
|
+
disabled: false,
|
|
81
|
+
softDisabled: false,
|
|
82
|
+
type: "button"
|
|
83
|
+
};
|
|
84
|
+
function createIconButton(options) {
|
|
85
|
+
const button = createButton({
|
|
86
|
+
...options,
|
|
87
|
+
props: () => ({
|
|
88
|
+
...options.props(),
|
|
89
|
+
variant: options.props().variant
|
|
90
|
+
})
|
|
91
|
+
});
|
|
92
|
+
return {
|
|
93
|
+
rootAttrs() {
|
|
94
|
+
const p = options.props();
|
|
95
|
+
return {
|
|
96
|
+
...button.rootAttrs(),
|
|
97
|
+
"aria-label": p.label,
|
|
98
|
+
"data-width": p.width ?? iconButtonDefaults.width,
|
|
99
|
+
"data-variant": p.variant ?? iconButtonDefaults.variant
|
|
100
|
+
};
|
|
101
|
+
},
|
|
102
|
+
handleClick: button.handleClick
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
// src/primitives/drag/drag.ts
|
|
106
|
+
/** Velocity is measured over this window, so a pause before release reads as slow. */
|
|
107
|
+
const VELOCITY_WINDOW_MS = 100;
|
|
108
|
+
/** How long to wait for the click that follows a drag before giving up on swallowing it. */
|
|
109
|
+
const CLICK_WINDOW_MS = 400;
|
|
110
|
+
const isDisabled$1 = (el) => el.matches(":disabled, [aria-disabled=\"true\"]");
|
|
111
|
+
function attachDrag(element, options) {
|
|
112
|
+
const pointerTypes = options.pointerTypes ?? ["touch", "pen"];
|
|
113
|
+
const threshold = options.threshold ?? 8;
|
|
114
|
+
let pointerId;
|
|
115
|
+
let start = {
|
|
116
|
+
x: 0,
|
|
117
|
+
y: 0
|
|
118
|
+
};
|
|
119
|
+
let samples = [];
|
|
120
|
+
let dragging = false;
|
|
121
|
+
const info = (event) => {
|
|
122
|
+
const first = samples[0];
|
|
123
|
+
const last = samples.at(-1);
|
|
124
|
+
const seconds = first && last ? (last.t - first.t) / 1e3 : 0;
|
|
125
|
+
return {
|
|
126
|
+
dx: event.clientX - start.x,
|
|
127
|
+
dy: event.clientY - start.y,
|
|
128
|
+
velocityX: seconds > 0 ? (last.x - first.x) / seconds : 0,
|
|
129
|
+
velocityY: seconds > 0 ? (last.y - first.y) / seconds : 0
|
|
130
|
+
};
|
|
131
|
+
};
|
|
132
|
+
const record = (event) => {
|
|
133
|
+
samples.push({
|
|
134
|
+
t: event.timeStamp,
|
|
135
|
+
x: event.clientX,
|
|
136
|
+
y: event.clientY
|
|
137
|
+
});
|
|
138
|
+
samples = samples.filter((s) => event.timeStamp - s.t <= VELOCITY_WINDOW_MS);
|
|
139
|
+
};
|
|
140
|
+
const reset = () => {
|
|
141
|
+
pointerId = void 0;
|
|
142
|
+
samples = [];
|
|
143
|
+
dragging = false;
|
|
144
|
+
delete element.dataset.dragging;
|
|
145
|
+
};
|
|
146
|
+
const swallowNextClick = () => {
|
|
147
|
+
const swallow = (event) => {
|
|
148
|
+
event.preventDefault();
|
|
149
|
+
event.stopImmediatePropagation();
|
|
150
|
+
};
|
|
151
|
+
element.addEventListener("click", swallow, {
|
|
152
|
+
capture: true,
|
|
153
|
+
once: true
|
|
154
|
+
});
|
|
155
|
+
setTimeout(() => element.removeEventListener("click", swallow, { capture: true }), CLICK_WINDOW_MS);
|
|
156
|
+
};
|
|
157
|
+
const onPointerDown = (event) => {
|
|
158
|
+
if (!event.isPrimary || !pointerTypes.includes(event.pointerType) || isDisabled$1(element)) return;
|
|
159
|
+
pointerId = event.pointerId;
|
|
160
|
+
start = {
|
|
161
|
+
x: event.clientX,
|
|
162
|
+
y: event.clientY
|
|
163
|
+
};
|
|
164
|
+
samples = [];
|
|
165
|
+
record(event);
|
|
166
|
+
};
|
|
167
|
+
const onPointerMove = (event) => {
|
|
168
|
+
if (event.pointerId !== pointerId) return;
|
|
169
|
+
record(event);
|
|
170
|
+
if (!dragging) {
|
|
171
|
+
const { dx, dy } = info(event);
|
|
172
|
+
if (Math.hypot(dx, dy) < threshold) return;
|
|
173
|
+
dragging = true;
|
|
174
|
+
try {
|
|
175
|
+
element.setPointerCapture(event.pointerId);
|
|
176
|
+
} catch {}
|
|
177
|
+
element.dataset.dragging = "";
|
|
178
|
+
options.onStart?.();
|
|
179
|
+
}
|
|
180
|
+
options.onMove(info(event));
|
|
181
|
+
};
|
|
182
|
+
const onPointerUp = (event) => {
|
|
183
|
+
if (event.pointerId !== pointerId) return;
|
|
184
|
+
if (dragging) {
|
|
185
|
+
record(event);
|
|
186
|
+
options.onEnd(info(event));
|
|
187
|
+
swallowNextClick();
|
|
188
|
+
}
|
|
189
|
+
reset();
|
|
190
|
+
};
|
|
191
|
+
const onPointerCancel = (event) => {
|
|
192
|
+
if (event.pointerId !== pointerId) return;
|
|
193
|
+
if (dragging) (options.onCancel ?? options.onEnd)(info(event));
|
|
194
|
+
reset();
|
|
195
|
+
};
|
|
196
|
+
element.addEventListener("pointerdown", onPointerDown);
|
|
197
|
+
element.addEventListener("pointermove", onPointerMove);
|
|
198
|
+
element.addEventListener("pointerup", onPointerUp);
|
|
199
|
+
element.addEventListener("pointercancel", onPointerCancel);
|
|
200
|
+
return { detach() {
|
|
201
|
+
reset();
|
|
202
|
+
element.removeEventListener("pointerdown", onPointerDown);
|
|
203
|
+
element.removeEventListener("pointermove", onPointerMove);
|
|
204
|
+
element.removeEventListener("pointerup", onPointerUp);
|
|
205
|
+
element.removeEventListener("pointercancel", onPointerCancel);
|
|
206
|
+
} };
|
|
207
|
+
}
|
|
208
|
+
// src/primitives/ripple/ripple.ts
|
|
209
|
+
/** Touch presses wait this long, so a scroll that starts on the element doesn't ripple. */
|
|
210
|
+
const TOUCH_DELAY_MS = 150;
|
|
211
|
+
/** A press stays visible at least this long, even for a quick tap. */
|
|
212
|
+
const MINIMUM_PRESS_MS = 225;
|
|
213
|
+
const GROW_MS = 450;
|
|
214
|
+
/** M3's legacy "standard" easing, which Material Web's ripple uses. */
|
|
215
|
+
const GROW_EASING = "cubic-bezier(0.2, 0, 0, 1)";
|
|
216
|
+
/** The ripple starts at this fraction of the element's larger side. */
|
|
217
|
+
const INITIAL_SCALE = .2;
|
|
218
|
+
const PADDING = 10;
|
|
219
|
+
const SOFT_EDGE_RATIO = .35;
|
|
220
|
+
const SOFT_EDGE_MINIMUM = 75;
|
|
221
|
+
const isDisabled = (el) => el.matches(":disabled, [aria-disabled=\"true\"]");
|
|
222
|
+
const reducedMotion$1 = () => typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
223
|
+
const setPressed = (els, pressed) => {
|
|
224
|
+
for (const el of els) if (pressed) el.dataset.pressed = "";
|
|
225
|
+
else delete el.dataset.pressed;
|
|
226
|
+
};
|
|
227
|
+
/**
|
|
228
|
+
* Adds a press ripple to an element. While pressed, the element (and its surface) has
|
|
229
|
+
* `data-pressed`. Load `@rio-m3e/styles/base/ripple.css` for the visuals; it styles elements
|
|
230
|
+
* marked `data-ripple`.
|
|
231
|
+
*/
|
|
232
|
+
function attachRipple(element, options = {}) {
|
|
233
|
+
const surface = options.surface ?? element;
|
|
234
|
+
const marked = surface === element ? [element] : [element, surface];
|
|
235
|
+
let pointerId;
|
|
236
|
+
let touchTimer;
|
|
237
|
+
let releaseTimer;
|
|
238
|
+
let animation;
|
|
239
|
+
let pressedAt = 0;
|
|
240
|
+
const start = (event) => {
|
|
241
|
+
const { width, height, left, top } = surface.getBoundingClientRect();
|
|
242
|
+
const larger = Math.max(width, height);
|
|
243
|
+
const softEdge = Math.max(SOFT_EDGE_RATIO * larger, SOFT_EDGE_MINIMUM);
|
|
244
|
+
const from = event ? {
|
|
245
|
+
x: event.clientX - left,
|
|
246
|
+
y: event.clientY - top
|
|
247
|
+
} : {
|
|
248
|
+
x: width / 2,
|
|
249
|
+
y: height / 2
|
|
250
|
+
};
|
|
251
|
+
const to = {
|
|
252
|
+
x: width / 2,
|
|
253
|
+
y: height / 2
|
|
254
|
+
};
|
|
255
|
+
const radius = {
|
|
256
|
+
from: larger * INITIAL_SCALE / 2,
|
|
257
|
+
to: (Math.hypot(width, height) + PADDING + softEdge) / 2
|
|
258
|
+
};
|
|
259
|
+
const frame = (point, r) => ({
|
|
260
|
+
"--rio-ripple-x": `${point.x}px`,
|
|
261
|
+
"--rio-ripple-y": `${point.y}px`,
|
|
262
|
+
"--rio-ripple-radius": `${r}px`
|
|
263
|
+
});
|
|
264
|
+
animation?.cancel();
|
|
265
|
+
animation = surface.animate([frame(from, radius.from), frame(to, radius.to)], {
|
|
266
|
+
pseudoElement: "::after",
|
|
267
|
+
duration: reducedMotion$1() ? 0 : GROW_MS,
|
|
268
|
+
easing: GROW_EASING,
|
|
269
|
+
fill: "forwards"
|
|
270
|
+
});
|
|
271
|
+
pressedAt = performance.now();
|
|
272
|
+
setPressed(marked, true);
|
|
273
|
+
};
|
|
274
|
+
const end = () => {
|
|
275
|
+
pointerId = void 0;
|
|
276
|
+
clearTimeout(touchTimer);
|
|
277
|
+
touchTimer = void 0;
|
|
278
|
+
if (!("pressed" in element.dataset)) return;
|
|
279
|
+
const current = animation;
|
|
280
|
+
const remaining = MINIMUM_PRESS_MS - (performance.now() - pressedAt);
|
|
281
|
+
const release = () => {
|
|
282
|
+
if (animation === current) setPressed(marked, false);
|
|
283
|
+
};
|
|
284
|
+
if (remaining > 0) releaseTimer = setTimeout(release, remaining);
|
|
285
|
+
else release();
|
|
286
|
+
};
|
|
287
|
+
const isOwnPointer = (event) => event.isPrimary && (pointerId === void 0 || pointerId === event.pointerId);
|
|
288
|
+
const onPointerDown = (event) => {
|
|
289
|
+
if (isDisabled(element) || !event.isPrimary) return;
|
|
290
|
+
const touch = event.pointerType === "touch";
|
|
291
|
+
if (!touch && event.buttons !== 1) return;
|
|
292
|
+
pointerId = event.pointerId;
|
|
293
|
+
if (!touch) {
|
|
294
|
+
start(event);
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
touchTimer = setTimeout(() => {
|
|
298
|
+
touchTimer = void 0;
|
|
299
|
+
start(event);
|
|
300
|
+
}, TOUCH_DELAY_MS);
|
|
301
|
+
};
|
|
302
|
+
const onPointerUp = (event) => {
|
|
303
|
+
if (!isOwnPointer(event) || pointerId === void 0) return;
|
|
304
|
+
if (touchTimer !== void 0) start(event);
|
|
305
|
+
end();
|
|
306
|
+
};
|
|
307
|
+
const onPointerLeave = (event) => {
|
|
308
|
+
if (event.pointerType !== "touch" && isOwnPointer(event)) end();
|
|
309
|
+
};
|
|
310
|
+
const onCancel = (event) => {
|
|
311
|
+
if (event instanceof PointerEvent && !isOwnPointer(event)) return;
|
|
312
|
+
end();
|
|
313
|
+
};
|
|
314
|
+
const onClick = (event) => {
|
|
315
|
+
if (event.detail !== 0 || isDisabled(element)) return;
|
|
316
|
+
start();
|
|
317
|
+
end();
|
|
318
|
+
};
|
|
319
|
+
surface.dataset.ripple = "";
|
|
320
|
+
element.addEventListener("pointerdown", onPointerDown);
|
|
321
|
+
element.addEventListener("pointerup", onPointerUp);
|
|
322
|
+
element.addEventListener("pointerleave", onPointerLeave);
|
|
323
|
+
element.addEventListener("pointercancel", onCancel);
|
|
324
|
+
element.addEventListener("contextmenu", onCancel);
|
|
325
|
+
element.addEventListener("click", onClick);
|
|
326
|
+
return { detach() {
|
|
327
|
+
clearTimeout(touchTimer);
|
|
328
|
+
clearTimeout(releaseTimer);
|
|
329
|
+
animation?.cancel();
|
|
330
|
+
delete surface.dataset.ripple;
|
|
331
|
+
setPressed(marked, false);
|
|
332
|
+
element.removeEventListener("pointerdown", onPointerDown);
|
|
333
|
+
element.removeEventListener("pointerup", onPointerUp);
|
|
334
|
+
element.removeEventListener("pointerleave", onPointerLeave);
|
|
335
|
+
element.removeEventListener("pointercancel", onCancel);
|
|
336
|
+
element.removeEventListener("contextmenu", onCancel);
|
|
337
|
+
element.removeEventListener("click", onClick);
|
|
338
|
+
} };
|
|
339
|
+
}
|
|
340
|
+
// src/primitives/spring/spring.ts
|
|
341
|
+
/**
|
|
342
|
+
* Where a spring is `t` seconds after it starts at `from` with `velocity`, heading to `to`.
|
|
343
|
+
*/
|
|
344
|
+
function solveSpring({ dampingRatio: zeta, stiffness }, from, to, velocity, t) {
|
|
345
|
+
const w0 = Math.sqrt(stiffness);
|
|
346
|
+
const x0 = from - to;
|
|
347
|
+
const v0 = velocity;
|
|
348
|
+
if (Math.abs(zeta - 1) < 1e-6) {
|
|
349
|
+
const b = v0 + w0 * x0;
|
|
350
|
+
const decay = Math.exp(-w0 * t);
|
|
351
|
+
return {
|
|
352
|
+
value: to + (x0 + b * t) * decay,
|
|
353
|
+
velocity: (b - w0 * (x0 + b * t)) * decay
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
if (zeta < 1) {
|
|
357
|
+
const a = zeta * w0;
|
|
358
|
+
const wd = w0 * Math.sqrt(1 - zeta * zeta);
|
|
359
|
+
const b = (v0 + a * x0) / wd;
|
|
360
|
+
const decay = Math.exp(-a * t);
|
|
361
|
+
const cos = Math.cos(wd * t);
|
|
362
|
+
const sin = Math.sin(wd * t);
|
|
363
|
+
return {
|
|
364
|
+
value: to + decay * (x0 * cos + b * sin),
|
|
365
|
+
velocity: decay * ((wd * b - a * x0) * cos - (a * b + wd * x0) * sin)
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
const root = w0 * Math.sqrt(zeta * zeta - 1);
|
|
369
|
+
const r1 = -zeta * w0 + root;
|
|
370
|
+
const r2 = -zeta * w0 - root;
|
|
371
|
+
const c2 = (v0 - r1 * x0) / (r2 - r1);
|
|
372
|
+
const c1 = x0 - c2;
|
|
373
|
+
const e1 = Math.exp(r1 * t);
|
|
374
|
+
const e2 = Math.exp(r2 * t);
|
|
375
|
+
return {
|
|
376
|
+
value: to + c1 * e1 + c2 * e2,
|
|
377
|
+
velocity: r1 * c1 * e1 + r2 * c2 * e2
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
const now = () => performance.now() / 1e3;
|
|
381
|
+
/** A spring that animates a single number with `requestAnimationFrame`. */
|
|
382
|
+
function createSpring(options) {
|
|
383
|
+
const precision = options.precision ?? .01;
|
|
384
|
+
let from = options.value;
|
|
385
|
+
let target = options.value;
|
|
386
|
+
let startVelocity = 0;
|
|
387
|
+
let params;
|
|
388
|
+
let startedAt = 0;
|
|
389
|
+
let frame;
|
|
390
|
+
const state = () => params && frame !== void 0 ? solveSpring(params, from, target, startVelocity, now() - startedAt) : {
|
|
391
|
+
value: target,
|
|
392
|
+
velocity: 0
|
|
393
|
+
};
|
|
394
|
+
const settle = () => {
|
|
395
|
+
if (frame !== void 0) cancelAnimationFrame(frame);
|
|
396
|
+
frame = void 0;
|
|
397
|
+
from = target;
|
|
398
|
+
startVelocity = 0;
|
|
399
|
+
options.onUpdate(target);
|
|
400
|
+
options.onRest?.();
|
|
401
|
+
};
|
|
402
|
+
const tick = () => {
|
|
403
|
+
const { value, velocity } = state();
|
|
404
|
+
if (Math.abs(value - target) < precision && Math.abs(velocity) < precision * 10) {
|
|
405
|
+
settle();
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
options.onUpdate(value);
|
|
409
|
+
frame = requestAnimationFrame(tick);
|
|
410
|
+
};
|
|
411
|
+
return {
|
|
412
|
+
get value() {
|
|
413
|
+
return state().value;
|
|
414
|
+
},
|
|
415
|
+
get velocity() {
|
|
416
|
+
return state().velocity;
|
|
417
|
+
},
|
|
418
|
+
get target() {
|
|
419
|
+
return target;
|
|
420
|
+
},
|
|
421
|
+
get moving() {
|
|
422
|
+
return frame !== void 0;
|
|
423
|
+
},
|
|
424
|
+
to(next, nextParams, velocity) {
|
|
425
|
+
const current = state();
|
|
426
|
+
from = current.value;
|
|
427
|
+
startVelocity = velocity ?? current.velocity;
|
|
428
|
+
target = next;
|
|
429
|
+
params = nextParams;
|
|
430
|
+
startedAt = now();
|
|
431
|
+
if (frame === void 0) frame = requestAnimationFrame(tick);
|
|
432
|
+
},
|
|
433
|
+
jump(value) {
|
|
434
|
+
target = value;
|
|
435
|
+
settle();
|
|
436
|
+
},
|
|
437
|
+
stop() {
|
|
438
|
+
const { value } = state();
|
|
439
|
+
if (frame !== void 0) cancelAnimationFrame(frame);
|
|
440
|
+
frame = void 0;
|
|
441
|
+
from = target = value;
|
|
442
|
+
startVelocity = 0;
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Reads a motion token's spring from CSS variables on an element, for example
|
|
448
|
+
* `--rio-motion-fast-spatial-damping` and `-stiffness`. Reading them from the element, rather than
|
|
449
|
+
* from the tokens package, lets a subtree that swaps motion schemes change its springs too.
|
|
450
|
+
* Returns `undefined` when the theme doesn't define them.
|
|
451
|
+
*/
|
|
452
|
+
function readSpringParams(element, token) {
|
|
453
|
+
const style = getComputedStyle(element);
|
|
454
|
+
const dampingRatio = parseFloat(style.getPropertyValue(`--rio-motion-${token}-damping`));
|
|
455
|
+
const stiffness = parseFloat(style.getPropertyValue(`--rio-motion-${token}-stiffness`));
|
|
456
|
+
return Number.isFinite(dampingRatio) && Number.isFinite(stiffness) ? {
|
|
457
|
+
dampingRatio,
|
|
458
|
+
stiffness
|
|
459
|
+
} : void 0;
|
|
460
|
+
}
|
|
461
|
+
// src/primitives/spring-property/spring-property.ts
|
|
462
|
+
/** The data attributes Rio's components use for state. */
|
|
463
|
+
const STATE_ATTRIBUTES = [
|
|
464
|
+
"data-pressed",
|
|
465
|
+
"data-dragging",
|
|
466
|
+
"data-selected",
|
|
467
|
+
"data-shape",
|
|
468
|
+
"data-size",
|
|
469
|
+
"data-icon"
|
|
470
|
+
];
|
|
471
|
+
const reducedMotion = () => typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
472
|
+
function attachSpringProperty(element, options) {
|
|
473
|
+
const token = options.token ?? "fast-spatial";
|
|
474
|
+
const read = () => parseFloat(getComputedStyle(element).getPropertyValue(options.target));
|
|
475
|
+
const initial = read();
|
|
476
|
+
let held = false;
|
|
477
|
+
const spring = createSpring({
|
|
478
|
+
value: Number.isFinite(initial) ? initial : 0,
|
|
479
|
+
onUpdate: (value) => options.apply(value),
|
|
480
|
+
onRest: () => {
|
|
481
|
+
if (!held) options.apply(void 0);
|
|
482
|
+
}
|
|
483
|
+
});
|
|
484
|
+
const go = (velocity) => {
|
|
485
|
+
const target = read();
|
|
486
|
+
if (!Number.isFinite(target)) return;
|
|
487
|
+
const params = readSpringParams(element, token);
|
|
488
|
+
if (!params || reducedMotion()) spring.jump(target);
|
|
489
|
+
else spring.to(target, params, velocity);
|
|
490
|
+
};
|
|
491
|
+
const observer = new MutationObserver(() => {
|
|
492
|
+
if (!held && read() !== spring.target) go();
|
|
493
|
+
});
|
|
494
|
+
observer.observe(element, {
|
|
495
|
+
attributes: true,
|
|
496
|
+
attributeFilter: options.watch ?? STATE_ATTRIBUTES
|
|
497
|
+
});
|
|
498
|
+
return {
|
|
499
|
+
get value() {
|
|
500
|
+
return spring.value;
|
|
501
|
+
},
|
|
502
|
+
hold(value) {
|
|
503
|
+
held = true;
|
|
504
|
+
spring.jump(value);
|
|
505
|
+
},
|
|
506
|
+
release(velocity = 0) {
|
|
507
|
+
held = false;
|
|
508
|
+
go(velocity);
|
|
509
|
+
},
|
|
510
|
+
detach() {
|
|
511
|
+
observer.disconnect();
|
|
512
|
+
spring.stop();
|
|
513
|
+
options.apply(void 0);
|
|
514
|
+
}
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
// src/primitives/shape-morph/shape-morph.ts
|
|
518
|
+
function attachShapeMorph(element, options = {}) {
|
|
519
|
+
const radius = attachSpringProperty(element, {
|
|
520
|
+
...options,
|
|
521
|
+
target: "--rio-shape-morph-target",
|
|
522
|
+
apply: (value) => value === void 0 ? element.style.removeProperty("border-radius") : element.style.setProperty("border-radius", `${value}px`)
|
|
523
|
+
});
|
|
524
|
+
element.dataset.shapeMorph = "";
|
|
525
|
+
return { detach() {
|
|
526
|
+
radius.detach();
|
|
527
|
+
delete element.dataset.shapeMorph;
|
|
528
|
+
} };
|
|
529
|
+
}
|
|
530
|
+
// src/switch/handle.ts
|
|
531
|
+
/** A release faster than this (px/s) picks the side it's heading to, wherever the handle is. */
|
|
532
|
+
const FLING_VELOCITY = 300;
|
|
533
|
+
const inlineVar = (el, name) => (value) => value === void 0 ? el.style.removeProperty(name) : el.style.setProperty(name, `${value}px`);
|
|
534
|
+
const readPx = (el, name) => parseFloat(getComputedStyle(el).getPropertyValue(name));
|
|
535
|
+
function attachSwitchHandle(root, options) {
|
|
536
|
+
const x = attachSpringProperty(root, {
|
|
537
|
+
target: "--rio-switch-handle-x-target",
|
|
538
|
+
apply: inlineVar(root, "--_handle-x")
|
|
539
|
+
});
|
|
540
|
+
const size = attachSpringProperty(root, {
|
|
541
|
+
target: "--rio-switch-handle-size-target",
|
|
542
|
+
apply: inlineVar(root, "--_handle-size")
|
|
543
|
+
});
|
|
544
|
+
let from = 0;
|
|
545
|
+
let off = 0;
|
|
546
|
+
let on = 0;
|
|
547
|
+
let direction = 1;
|
|
548
|
+
const drag = attachDrag(root, {
|
|
549
|
+
onStart() {
|
|
550
|
+
off = readPx(root, "--rio-switch-handle-x-off");
|
|
551
|
+
on = readPx(root, "--rio-switch-handle-x-on");
|
|
552
|
+
direction = getComputedStyle(root).direction === "rtl" ? -1 : 1;
|
|
553
|
+
from = x.value;
|
|
554
|
+
x.hold(from);
|
|
555
|
+
},
|
|
556
|
+
onMove({ dx }) {
|
|
557
|
+
x.hold(Math.min(on, Math.max(off, from + direction * dx)));
|
|
558
|
+
},
|
|
559
|
+
onEnd({ velocityX }) {
|
|
560
|
+
const velocity = direction * velocityX;
|
|
561
|
+
const next = Math.abs(velocity) > FLING_VELOCITY ? velocity > 0 : x.value > (off + on) / 2;
|
|
562
|
+
if (next !== options.selected()) options.onSelectedChange(next);
|
|
563
|
+
x.release(velocity);
|
|
564
|
+
},
|
|
565
|
+
onCancel() {
|
|
566
|
+
x.release();
|
|
567
|
+
}
|
|
568
|
+
});
|
|
569
|
+
return { detach() {
|
|
570
|
+
drag.detach();
|
|
571
|
+
x.detach();
|
|
572
|
+
size.detach();
|
|
573
|
+
} };
|
|
574
|
+
}
|
|
575
|
+
// src/switch/switch.ts
|
|
576
|
+
const switchDefaults = {
|
|
577
|
+
selected: false,
|
|
578
|
+
disabled: false,
|
|
579
|
+
softDisabled: false
|
|
580
|
+
};
|
|
581
|
+
function createSwitch(options) {
|
|
582
|
+
return {
|
|
583
|
+
rootAttrs() {
|
|
584
|
+
const p = options.props();
|
|
585
|
+
const selected = String(!!p.selected);
|
|
586
|
+
return {
|
|
587
|
+
type: "button",
|
|
588
|
+
role: "switch",
|
|
589
|
+
"aria-checked": selected,
|
|
590
|
+
disabled: p.disabled ? "" : void 0,
|
|
591
|
+
"aria-disabled": p.softDisabled && !p.disabled ? "true" : void 0,
|
|
592
|
+
"data-selected": selected
|
|
593
|
+
};
|
|
594
|
+
},
|
|
595
|
+
handleClick(event) {
|
|
596
|
+
const p = options.props();
|
|
597
|
+
if (p.disabled || p.softDisabled) {
|
|
598
|
+
event.preventDefault();
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
options.onSelectedChange(!p.selected);
|
|
602
|
+
}
|
|
603
|
+
};
|
|
604
|
+
}
|
|
605
|
+
export { BUTTON_SIZES, BUTTON_VARIANTS, ICON_BUTTON_VARIANTS, ICON_BUTTON_WIDTHS, STATE_ATTRIBUTES, attachDrag, attachRipple, attachShapeMorph, attachSpringProperty, attachSwitchHandle, buttonDefaults, createButton, createIconButton, createSpring, createSwitch, iconButtonDefaults, readSpringParams, solveSpring, switchDefaults };
|
package/package.json
CHANGED
|
@@ -1,6 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rio-m3e/core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Framework-agnostic state, props and behaviors for Rio",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": "LegenLee",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/LegenLee/rio.git",
|
|
10
|
+
"directory": "packages/core"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"NOTICE"
|
|
15
|
+
],
|
|
16
|
+
"type": "module",
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"@rio-m3e/source": "./src/index.ts",
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./package.json": "./package.json"
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"tsdown": "0.23.7",
|
|
31
|
+
"typescript": "~6.0.3"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsdown",
|
|
35
|
+
"typecheck": "tsc -p ."
|
|
36
|
+
}
|
|
6
37
|
}
|