@geoffcox/sterling-svelte 0.0.9 → 0.0.11

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,219 @@
1
+ <script>import { v4 as uuid } from "uuid";
2
+ import Label from "../display/Label.svelte";
3
+ export let checked = false;
4
+ export let disabled = false;
5
+ export let vertical = false;
6
+ export let onText = "";
7
+ export let offText = "";
8
+ const inputId = uuid();
9
+ let switchWidth = 0;
10
+ let switchHeight = 0;
11
+ let thumbWidth = 0;
12
+ let thumbHeight = 0;
13
+ $:
14
+ switchSize = vertical ? switchHeight : switchWidth;
15
+ $:
16
+ thumbSize = vertical ? thumbHeight : thumbWidth;
17
+ $:
18
+ ratio = checked ? 1 : 0;
19
+ $:
20
+ valueOffset = (switchSize - thumbSize) * ratio;
21
+ </script>
22
+
23
+ <!--
24
+ @component
25
+ A styled HTML input type=checkbox element.
26
+ -->
27
+ <div class="sterling-switch" class:vertical class:disabled>
28
+ <input
29
+ type="checkbox"
30
+ on:blur
31
+ on:click
32
+ on:change
33
+ on:dblclick
34
+ on:focus
35
+ on:focusin
36
+ on:focusout
37
+ on:keydown
38
+ on:keypress
39
+ on:keyup
40
+ on:input
41
+ on:mousedown
42
+ on:mouseenter
43
+ on:mouseleave
44
+ on:mousemove
45
+ on:mouseover
46
+ on:mouseout
47
+ on:mouseup
48
+ on:toggle
49
+ on:wheel
50
+ bind:checked
51
+ id={inputId}
52
+ {disabled}
53
+ {...$$restProps}
54
+ />
55
+ <div class="off-label">
56
+ <slot name="off-label" {checked} {disabled} {inputId} {offText} {vertical}>
57
+ {#if offText}
58
+ <Label for={inputId} {disabled}>{offText}</Label>
59
+ {/if}
60
+ </slot>
61
+ </div>
62
+ <!-- svelte-ignore a11y-click-events-have-key-events -->
63
+ <div class="switch" bind:offsetWidth={switchWidth} bind:offsetHeight={switchHeight}>
64
+ <div
65
+ class="thumb"
66
+ bind:offsetWidth={thumbWidth}
67
+ bind:offsetHeight={thumbHeight}
68
+ style={`--thumb-offset: ${valueOffset}px`}
69
+ />
70
+ </div>
71
+ <div class="on-label">
72
+ <slot name="on-label" {checked} {disabled} {inputId} {onText} {vertical}>
73
+ {#if onText}
74
+ <Label for={inputId} {disabled}>{onText}</Label>
75
+ {/if}
76
+ </slot>
77
+ </div>
78
+ </div>
79
+
80
+ <style>
81
+ .sterling-switch {
82
+ cursor: pointer;
83
+ display: grid;
84
+ position: relative;
85
+ }
86
+
87
+ .sterling-switch:not(.vertical) {
88
+ align-items: center;
89
+ column-gap: 0.5em;
90
+ grid-template-columns: auto auto auto;
91
+ grid-template-rows: auto;
92
+ justify-items: stretch;
93
+ }
94
+
95
+ .sterling-switch.vertical {
96
+ align-items: stretch;
97
+ grid-template-columns: auto;
98
+ grid-template-rows: auto auto auto;
99
+ justify-items: center;
100
+ row-gap: 0.5em;
101
+ }
102
+
103
+ .sterling-switch.vertical .off-label {
104
+ grid-row: 3 / span 1;
105
+ }
106
+
107
+ .sterling-switch.vertical .on-label {
108
+ grid-row: 1 / span 1;
109
+ }
110
+
111
+ input {
112
+ font: inherit;
113
+ margin: 0;
114
+ padding: 0;
115
+ position: absolute;
116
+ left: 0;
117
+ right: 0;
118
+ bottom: 0;
119
+ top: 0;
120
+ opacity: 0;
121
+ }
122
+
123
+ .switch {
124
+ background-color: var(--Input__background-color);
125
+ border-color: var(--Input__border-color);
126
+ border-radius: 10000px;
127
+ border-style: var(--Input__border-style);
128
+ border-width: var(--Input__border-width);
129
+ box-sizing: border-box;
130
+ color: var(--Input__color);
131
+ font: inherit;
132
+ pointer-events: none;
133
+ position: relative;
134
+ transition: background-color 250ms, color 250ms, border-color 250ms;
135
+ user-select: none;
136
+ }
137
+
138
+ .sterling-switch:hover .switch {
139
+ background-color: var(--Input__background-color--hover);
140
+ border-color: var(--Input__border-color--hover);
141
+ color: var(--Input__color--hover);
142
+ }
143
+
144
+ input:focus-visible ~ .switch {
145
+ background-color: var(--Input__background-color--focus);
146
+ border-color: var(--Input__border-color--focus);
147
+ color: var(--Common__color--focux);
148
+ outline-color: var(--Common__outline-color);
149
+ outline-offset: var(--Common__outline-offset);
150
+ outline-style: var(--Common__outline-style);
151
+ outline-width: var(--Common__outline-width);
152
+ }
153
+
154
+ .sterling-switch.disabled .switch {
155
+ background-color: var(--Input__background-color--disabled);
156
+ border-color: var(--Input__border-color--disabled);
157
+ color: var(--Input__color--disabled);
158
+ }
159
+
160
+ .sterling-switch:not(.vertical) .switch {
161
+ width: 2em;
162
+ height: 1.25em;
163
+ }
164
+
165
+ .sterling-switch.vertical .switch {
166
+ width: 1.25em;
167
+ height: 2em;
168
+ }
169
+
170
+ .off-label,
171
+ .on-label {
172
+ padding-top: var(--Button__border-width);
173
+ }
174
+
175
+ .thumb {
176
+ background-color: var(--Button__background-color);
177
+ border-color: var(--Button__border-color);
178
+ border-radius: 10000px;
179
+ border-style: var(--Button__border-style);
180
+ border-width: var(--Button__border-width);
181
+ box-sizing: border-box;
182
+ color: var(--Button__color);
183
+ cursor: pointer;
184
+ display: block;
185
+ font: inherit;
186
+ height: 1.25em;
187
+ position: absolute;
188
+ transition: background-color 250ms, color 250ms, border-color 250ms, transform 250ms;
189
+ width: 1.25em;
190
+ }
191
+
192
+ .sterling-switch:hover .thumb {
193
+ background-color: var(--Button__background-color--hover);
194
+ border-color: var(--Button__border-color--hover);
195
+ color: var(--Button__color--hover);
196
+ }
197
+
198
+ .sterling-switch:active .thumb {
199
+ background-color: var(--Button__background-color--active);
200
+ border-color: var(--Button__border-color--active);
201
+ color: var(--Button__color--hover);
202
+ }
203
+
204
+ .sterling-switch.disabled .thumb {
205
+ background-color: var(--Button__background-color--disabled);
206
+ border-color: var(--Button__border-color--disabled);
207
+ color: var(--Button__color--disabled);
208
+ }
209
+
210
+ .sterling-switch:not(.vertical) .thumb {
211
+ top: calc(-1 * var(--Button__border-width));
212
+ transform: translateX(calc(var(--thumb-offset) - var(--Button__border-width)));
213
+ }
214
+
215
+ .sterling-switch.vertical .thumb {
216
+ left: calc(-1 * var(--Button__border-width));
217
+ transform: translateY(calc(var(--thumb-offset) - var(--Button__border-width)));
218
+ }
219
+ </style>
@@ -0,0 +1,58 @@
1
+ import { SvelteComponentTyped } from "svelte";
2
+ declare const __propDef: {
3
+ props: {
4
+ [x: string]: any;
5
+ checked?: boolean | undefined;
6
+ disabled?: boolean | undefined;
7
+ vertical?: boolean | undefined;
8
+ onText?: string | undefined;
9
+ offText?: string | undefined;
10
+ };
11
+ events: {
12
+ blur: FocusEvent;
13
+ click: MouseEvent;
14
+ change: Event;
15
+ dblclick: MouseEvent;
16
+ focus: FocusEvent;
17
+ focusin: FocusEvent;
18
+ focusout: FocusEvent;
19
+ keydown: KeyboardEvent;
20
+ keypress: KeyboardEvent;
21
+ keyup: KeyboardEvent;
22
+ input: Event;
23
+ mousedown: MouseEvent;
24
+ mouseenter: MouseEvent;
25
+ mouseleave: MouseEvent;
26
+ mousemove: MouseEvent;
27
+ mouseover: MouseEvent;
28
+ mouseout: MouseEvent;
29
+ mouseup: MouseEvent;
30
+ toggle: Event;
31
+ wheel: WheelEvent;
32
+ } & {
33
+ [evt: string]: CustomEvent<any>;
34
+ };
35
+ slots: {
36
+ 'off-label': {
37
+ checked: boolean;
38
+ disabled: boolean;
39
+ inputId: string;
40
+ offText: string;
41
+ vertical: boolean;
42
+ };
43
+ 'on-label': {
44
+ checked: boolean;
45
+ disabled: boolean;
46
+ inputId: string;
47
+ onText: string;
48
+ vertical: boolean;
49
+ };
50
+ };
51
+ };
52
+ export type SwitchProps = typeof __propDef.props;
53
+ export type SwitchEvents = typeof __propDef.events;
54
+ export type SwitchSlots = typeof __propDef.slots;
55
+ /** A styled HTML input type=checkbox element. */
56
+ export default class Switch extends SvelteComponentTyped<SwitchProps, SwitchEvents, SwitchSlots> {
57
+ }
58
+ export {};
package/lists/List.svelte CHANGED
@@ -1,4 +1,6 @@
1
1
  <script>import { createEventDispatcher } from "svelte";
2
+ import { v4 as uuid } from "uuid";
3
+ import Label from "../display/Label.svelte";
2
4
  export let disabled = false;
3
5
  export let items = [];
4
6
  export let horizontal = false;
@@ -7,6 +9,7 @@ export let selectedItem = void 0;
7
9
  $: {
8
10
  selectedItem = items[selectedIndex];
9
11
  }
12
+ const inputId = uuid();
10
13
  let listRef;
11
14
  let itemRefs = {};
12
15
  let focusVisible = false;
@@ -90,241 +93,162 @@ const onKeydown = (event) => {
90
93
  @component
91
94
  A list of items where a single item can be selected.
92
95
  -->
93
- {#if $$slots.label}
94
- <!-- svelte-ignore a11y-label-has-associated-control -->
95
- <label class="sterling-list-label" class:horizontal class:disabled>
96
- <div class="label-content" class:disabled>
97
- <slot name="label" />
98
- </div>
99
- <div
100
- bind:this={listRef}
101
- class="sterling-list labeled"
102
- class:disabled
103
- class:focus-visible={focusVisible}
104
- class:horizontal
105
- role="listbox"
106
- tabindex={!disabled ? 0 : undefined}
107
- on:blur
108
- on:click
109
- on:copy
110
- on:cut
111
- on:dblclick
112
- on:focus
113
- on:focusin
114
- on:focusout
115
- on:keydown={onKeydown}
116
- on:keydown
117
- on:keypress
118
- on:keyup
119
- on:mousedown
120
- on:mouseenter
121
- on:mouseleave
122
- on:mousemove
123
- on:mouseover
124
- on:mouseout
125
- on:mouseup
126
- on:scroll
127
- on:wheel
128
- on:paste
129
- {...$$restProps}
130
- >
131
- {#each items as item, index (item)}
132
- {@const selected = selectedIndex === index}
133
- <!-- svelte-ignore a11y-click-events-have-key-events -->
134
- <div
135
- bind:this={itemRefs[index]}
136
- aria-selected={disabled ? undefined : selected}
137
- class="list-item"
138
- class:selected
139
- class:disabled
140
- data-index={index + 1}
141
- role="option"
142
- on:click={() => onItemClick(index)}
143
- >
144
- <slot {disabled} {index} {item} {selected}>
145
- {item}
146
- </slot>
147
- </div>
148
- {/each}
149
- </div>
150
- </label>
151
- {:else}
152
- <div
153
- bind:this={listRef}
154
- class="sterling-list"
155
- class:disabled
156
- class:focus-visible={focusVisible}
157
- class:horizontal
158
- role="listbox"
159
- tabindex={!disabled ? 0 : undefined}
160
- on:blur
161
- on:click
162
- on:copy
163
- on:cut
164
- on:dblclick
165
- on:focus
166
- on:focusin
167
- on:focusout
168
- on:keydown={onKeydown}
169
- on:keydown
170
- on:keypress
171
- on:keyup
172
- on:mousedown
173
- on:mouseenter
174
- on:mouseleave
175
- on:mousemove
176
- on:mouseover
177
- on:mouseout
178
- on:mouseup
179
- on:scroll
180
- on:wheel
181
- on:paste
182
- {...$$restProps}
183
- >
184
- {#each items as item, index (item)}
185
- {@const selected = selectedIndex === index}
186
- <!-- svelte-ignore a11y-click-events-have-key-events -->
187
- <div
188
- bind:this={itemRefs[index]}
189
- aria-selected={disabled ? undefined : selected}
190
- class="list-item"
191
- class:selected
192
- class:disabled
193
- data-index={index + 1}
194
- role="option"
195
- on:click={() => onItemClick(index)}
196
- >
197
- <slot {disabled} {index} {item} {selected}>
198
- {item}
199
- </slot>
200
- </div>
201
- {/each}
202
- </div>
203
- {/if}
96
+ <div class="sterling-list" class:horizontal class:disabled>
97
+ {#if $$slots.label}
98
+ <div class="label">
99
+ <Label {disabled} for={inputId}>
100
+ <slot name="label" />
101
+ </Label>
102
+ </div>
103
+ {/if}
104
+ <div
105
+ bind:this={listRef}
106
+ class="list"
107
+ class:disabled
108
+ class:focus-visible={focusVisible}
109
+ class:horizontal
110
+ role="listbox"
111
+ tabindex={!disabled ? 0 : undefined}
112
+ on:blur
113
+ on:click
114
+ on:copy
115
+ on:cut
116
+ on:dblclick
117
+ on:focus
118
+ on:focusin
119
+ on:focusout
120
+ on:keydown={onKeydown}
121
+ on:keydown
122
+ on:keypress
123
+ on:keyup
124
+ on:mousedown
125
+ on:mouseenter
126
+ on:mouseleave
127
+ on:mousemove
128
+ on:mouseover
129
+ on:mouseout
130
+ on:mouseup
131
+ on:scroll
132
+ on:wheel
133
+ on:paste
134
+ {...$$restProps}
135
+ >
136
+ {#each items as item, index (item)}
137
+ {@const selected = selectedIndex === index}
138
+ <!-- svelte-ignore a11y-click-events-have-key-events -->
139
+ <div
140
+ bind:this={itemRefs[index]}
141
+ aria-selected={disabled ? undefined : selected}
142
+ class="list-item"
143
+ class:selected
144
+ class:disabled
145
+ data-index={index + 1}
146
+ role="option"
147
+ on:click={() => onItemClick(index)}
148
+ >
149
+ <slot {disabled} {index} {item} {selected}>
150
+ {item}
151
+ </slot>
152
+ </div>
153
+ {/each}
154
+ </div>
155
+ </div>
204
156
 
205
157
  <style>
206
- .sterling-list-label,
207
- .sterling-list {
208
- background-color: var(--Common__background-color);
209
- border-color: var(--Common__border-color);
210
- border-radius: var(--Common__border-radius);
211
- border-style: var(--Common__border-style);
212
- border-width: var(--Common__border-width);
213
- box-sizing: border-box;
214
- color: var(--Common__color);
215
- display: flex;
216
- flex-direction: column;
217
- flex-wrap: nowrap;
218
- height: 100%;
219
- margin: 0;
220
- padding: 0;
221
- transition: background-color 250ms, color 250ms, border-color 250ms;
222
- }
223
-
224
- .sterling-list-label.horizontal,
225
- .sterling-list.horizontal {
226
- height: unset;
227
- width: 100%;
228
- }
229
-
230
- .sterling-list {
231
- overflow-x: hidden;
232
- overflow-y: scroll;
233
- position: relative;
234
- }
158
+ .sterling-list {
159
+ background-color: var(--Common__background-color);
160
+ border-color: var(--Common__border-color);
161
+ border-radius: var(--Common__border-radius);
162
+ border-style: var(--Common__border-style);
163
+ border-width: var(--Common__border-width);
164
+ box-sizing: border-box;
165
+ color: var(--Common__color);
166
+ display: grid;
167
+ grid-template-columns: 1fr;
168
+ grid-template-rows: auto 1fr;
169
+ height: 100%;
170
+ margin: 0;
171
+ padding: 0;
172
+ transition: background-color 250ms, color 250ms, border-color 250ms;
173
+ }
235
174
 
236
- .sterling-list.horizontal {
237
- flex-direction: row;
238
- overflow-x: scroll;
239
- overflow-y: hidden;
240
- }
175
+ .sterling-list.horizontal {
176
+ height: unset;
177
+ width: 100%;
178
+ }
241
179
 
242
- .sterling-list-label:hover,
243
- .sterling-list:hover {
244
- border-color: var(--Common__border-color--hover);
245
- color: var(--Common__color--hover);
246
- }
180
+ .sterling-list:hover {
181
+ border-color: var(--Common__border-color--hover);
182
+ color: var(--Common__color--hover);
183
+ }
247
184
 
248
- .sterling-list-label:focus-visible,
249
- .sterling-list-label.focus-visible,
250
- .sterling-list:focus-visible,
251
- .sterling-list.focus-visible {
252
- border-color: var(--Common__border-color--focus);
253
- color: var(--Common__color--focus);
254
- outline-color: var(--Common__outline-color);
255
- outline-offset: var(--Common__outline-offset);
256
- outline-style: var(--Common__outline-style);
257
- outline-width: var(--Common__outline-width);
258
- }
185
+ .sterling-list:focus-visible,
186
+ .sterling-list.focus-visible {
187
+ border-color: var(--Common__border-color--focus);
188
+ color: var(--Common__color--focus);
189
+ outline-color: var(--Common__outline-color);
190
+ outline-offset: var(--Common__outline-offset);
191
+ outline-style: var(--Common__outline-style);
192
+ outline-width: var(--Common__outline-width);
193
+ }
259
194
 
260
- .sterling-list-label.disabled,
261
- .sterling-list.disabled {
262
- background-color: var(--Common__background-color--disabled);
263
- border-color: var(---Common__border-color--disabled);
264
- color: var(--Common__color--disabled);
265
- cursor: not-allowed;
266
- }
195
+ .sterling-list.disabled {
196
+ background-color: var(--Common__background-color--disabled);
197
+ border-color: var(---Common__border-color--disabled);
198
+ color: var(--Common__color--disabled);
199
+ cursor: not-allowed;
200
+ }
267
201
 
268
- /* The background, border, and outline are removed when labeled as the label provides them */
269
- .sterling-list.labeled,
270
- .sterling-list.labeled:hover,
271
- .sterling-list.labeled:focus-visible,
272
- .sterling-list.labeled.focus-visible,
273
- .sterling-list.labeled:disabled {
274
- background-color: transparent;
275
- border: none;
276
- outline: none;
277
- }
278
- .label {
279
- display: flex;
280
- flex-direction: column;
281
- box-sizing: border-box;
282
- }
202
+ .list {
203
+ display: flex;
204
+ flex-direction: column;
205
+ flex-wrap: nowrap;
206
+ grid-row: 2 / span 1;
207
+ overflow-x: hidden;
208
+ overflow-y: scroll;
209
+ position: relative;
210
+ }
283
211
 
284
- .label-content {
285
- font-size: 0.7em;
286
- margin: 0.5em 0.7em;
287
- color: var(--Display__color--subtle);
288
- transition: background-color 250ms, color 250ms, border-color 250ms;
289
- }
212
+ .list.horizontal {
213
+ flex-direction: row;
214
+ overflow-x: scroll;
215
+ overflow-y: hidden;
216
+ }
290
217
 
291
- .label-content.disabled {
292
- font-size: 0.7em;
293
- margin: 0.5em 0.7em;
294
- color: var(--Display__color--disabled);
295
- }
218
+ .label {
219
+ font-size: 0.7em;
220
+ margin: 0.5em 0.7em;
221
+ }
296
222
 
297
- .list-item {
298
- box-sizing: border-box;
299
- color: var(--Input__color);
300
- margin: 0;
301
- padding: 0.5em;
302
- outline: none;
303
- text-overflow: ellipsis;
304
- transition: background-color 250ms, color 250ms, border-color 250ms;
305
- white-space: nowrap;
306
- }
223
+ .list-item {
224
+ box-sizing: border-box;
225
+ color: var(--Input__color);
226
+ margin: 0;
227
+ padding: 0.5em;
228
+ outline: none;
229
+ text-overflow: ellipsis;
230
+ transition: background-color 250ms, color 250ms, border-color 250ms;
231
+ white-space: nowrap;
232
+ }
307
233
 
308
- .list-item:hover {
309
- background-color: var(--Button__background-color--hover);
310
- color: var(--Button__color--hover);
311
- }
234
+ .list-item:hover {
235
+ background-color: var(--Button__background-color--hover);
236
+ color: var(--Button__color--hover);
237
+ }
312
238
 
313
- .list-item.selected {
314
- background-color: var(--Input__background-color--selected);
315
- color: var(--Input__color--selected);
316
- }
239
+ .list-item.selected {
240
+ background-color: var(--Input__background-color--selected);
241
+ color: var(--Input__color--selected);
242
+ }
317
243
 
318
- .list-item.disabled {
319
- color: var(--Input__color--disabled);
320
- }
244
+ .list-item.disabled {
245
+ color: var(--Input__color--disabled);
246
+ }
321
247
 
322
- @media (prefers-reduced-motion) {
323
- .sterling-list-label,
324
- .sterling-list,
325
- .list-item,
326
- .label-content {
327
- transition: none;
328
- }
329
- }
248
+ @media (prefers-reduced-motion) {
249
+ .sterling-list,
250
+ .list-item {
251
+ transition: none;
252
+ }
253
+ }
330
254
  </style>