@spectrum-web-components/grid 0.0.2

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 ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,75 @@
1
+ ## Description
2
+
3
+ An `<sp-grid>` element displays a virtualized grid of elements built from its `items`, a normalized array of javascript objects, applied to a supplied `renderItem`, a `TemplateResult` returning method. `sp-grid` is a class extension of [`lit-virtualizer`](https://www.npmjs.com/package/@lit-labs/virtualizer/v/0.7.0-pre.2) and as such surfaces all of its underlying methods and events.
4
+
5
+ Elements displayed in the grid can be focused via the [roving tabindex](https://www.w3.org/TR/wai-aria-practices-1.2/#kbd_roving_tabindex) that allows the grid to be entered via the `Tab` key and then subsequent elements to be focused via the arrow keys. To inform the `<sp-grid>` element what part of the DOM created by the `renderItem` method can be focused, supply a value to `focusableSelector`. Focus will always enter the element list at index 0 of ALL available elements, not just those currently realized to the page.
6
+
7
+ Elements rendered via `renderItem` can have their width and height customized by supplying a value for `itemSize` that accepts an object: `{ width: number, height: number }`. You can customize the space between these elements via the `gap` property that accepts a value of `0` or `${number}px`.
8
+
9
+ ### Usage
10
+
11
+ [![See it on NPM!](https://img.shields.io/npm/v/@spectrum-web-components/grid?style=for-the-badge)](https://www.npmjs.com/package/@spectrum-web-components/grid)
12
+ [![How big is this package in your project?](https://img.shields.io/bundlephobia/minzip/@spectrum-web-components/grid?style=for-the-badge)](https://bundlephobia.com/result?p=@spectrum-web-components/grid)
13
+
14
+ ```
15
+ yarn add @spectrum-web-components/grid
16
+ ```
17
+
18
+ Import the side effectful registration of `<sp-grid>` via:
19
+
20
+ ```
21
+ import '@spectrum-web-components/grid/sp-grid.js';
22
+ ```
23
+
24
+ When looking to leverage the `Grid` base class as a type and/or for extension purposes, do so via:
25
+
26
+ ```
27
+ import { Grid } from '@spectrum-web-components/grid';
28
+ ```
29
+
30
+ ## Example
31
+
32
+ ```js
33
+ const items = generateItems(1000);
34
+
35
+ const renderItem = (
36
+ item: Item,
37
+ index: number,
38
+ selected: boolean
39
+ ): TemplateResult => {
40
+ return html`
41
+ <sp-card
42
+ toggles
43
+ variant="quiet"
44
+ heading="Card Heading ${item.id}"
45
+ subheading="JPG Photo"
46
+ style="contain: strict; padding: 1px;"
47
+ value="card-${item.id}"
48
+ .selected=${selected}
49
+ key=${index}
50
+ >
51
+ <img
52
+ alt=""
53
+ slot="preview"
54
+ src="https://picsum.photos/id/${item.id}/200/300"
55
+ decoding="async"
56
+ />
57
+ <div slot="description">10/15/18</div>
58
+ <div slot="footer">Footer</div>
59
+ </sp-card>
60
+ `;
61
+ };
62
+
63
+ // ...
64
+
65
+ <sp-grid
66
+ .items=${items}
67
+ .focusableSelector=${'sp-card'}
68
+ .renderItem=${renderItem}
69
+ .itemSize=${{
70
+ width: 200,
71
+ height: 300,
72
+ }}
73
+ .gap=${'10px'}
74
+ ></sp-grid>
75
+ ```
@@ -0,0 +1,411 @@
1
+ {
2
+ "schemaVersion": "1.0.0",
3
+ "readme": "",
4
+ "modules": [
5
+ {
6
+ "kind": "javascript-module",
7
+ "path": "sp-grid.ts",
8
+ "declarations": [],
9
+ "exports": [
10
+ {
11
+ "kind": "custom-element-definition",
12
+ "name": "sp-grid",
13
+ "declaration": {
14
+ "name": "Grid",
15
+ "module": "/src/Grid.js"
16
+ }
17
+ }
18
+ ]
19
+ },
20
+ {
21
+ "kind": "javascript-module",
22
+ "path": "src/Grid.ts",
23
+ "declarations": [
24
+ {
25
+ "kind": "class",
26
+ "description": "",
27
+ "name": "Grid",
28
+ "members": [
29
+ {
30
+ "kind": "field",
31
+ "name": "focusableSelector",
32
+ "type": {
33
+ "text": "string"
34
+ },
35
+ "privacy": "public",
36
+ "attribute": "focusableSelector"
37
+ },
38
+ {
39
+ "kind": "field",
40
+ "name": "gap",
41
+ "type": {
42
+ "text": "`${'0' | `${number}px`}`"
43
+ },
44
+ "privacy": "public",
45
+ "default": "'0'",
46
+ "attribute": "gap"
47
+ },
48
+ {
49
+ "kind": "field",
50
+ "name": "items",
51
+ "type": {
52
+ "text": "Record<string, unknown>[]"
53
+ },
54
+ "privacy": "public",
55
+ "default": "[]",
56
+ "attribute": "items"
57
+ },
58
+ {
59
+ "kind": "field",
60
+ "name": "itemSize",
61
+ "type": {
62
+ "text": "{\n width: number;\n height: number;\n }"
63
+ },
64
+ "privacy": "public",
65
+ "default": "{\n width: 200,\n height: 200,\n }",
66
+ "attribute": "itemSize"
67
+ },
68
+ {
69
+ "kind": "field",
70
+ "name": "renderItem",
71
+ "type": {
72
+ "text": "(item: unknown, index: number) => TemplateResult"
73
+ }
74
+ },
75
+ {
76
+ "kind": "field",
77
+ "name": "selected",
78
+ "type": {
79
+ "text": "Record<string, unknown>[]"
80
+ },
81
+ "privacy": "public",
82
+ "default": "[]",
83
+ "attribute": "selected"
84
+ },
85
+ {
86
+ "kind": "field",
87
+ "name": "gridController",
88
+ "default": "new GridController<HTMLElement>(this, {\n elements: () => [],\n itemSize: () => this.itemSize,\n /* c8 ignore next 1 */\n gap: () => this.gap,\n })"
89
+ },
90
+ {
91
+ "kind": "method",
92
+ "name": "handleChange",
93
+ "privacy": "protected",
94
+ "return": {
95
+ "type": {
96
+ "text": "void"
97
+ }
98
+ },
99
+ "parameters": [
100
+ {
101
+ "name": "event",
102
+ "type": {
103
+ "text": "Event"
104
+ }
105
+ }
106
+ ]
107
+ }
108
+ ],
109
+ "events": [
110
+ {
111
+ "description": "Announces that the value of `selected` has changed",
112
+ "name": "change"
113
+ }
114
+ ],
115
+ "attributes": [
116
+ {
117
+ "name": "focusableSelector",
118
+ "type": {
119
+ "text": "string"
120
+ },
121
+ "fieldName": "focusableSelector"
122
+ },
123
+ {
124
+ "name": "gap",
125
+ "type": {
126
+ "text": "`${'0' | `${number}px`}`"
127
+ },
128
+ "default": "'0'",
129
+ "fieldName": "gap"
130
+ },
131
+ {
132
+ "name": "items",
133
+ "type": {
134
+ "text": "Record<string, unknown>[]"
135
+ },
136
+ "default": "[]",
137
+ "fieldName": "items"
138
+ },
139
+ {
140
+ "name": "itemSize",
141
+ "type": {
142
+ "text": "{\n width: number;\n height: number;\n }"
143
+ },
144
+ "default": "{\n width: 200,\n height: 200,\n }",
145
+ "fieldName": "itemSize"
146
+ },
147
+ {
148
+ "name": "selected",
149
+ "type": {
150
+ "text": "Record<string, unknown>[]"
151
+ },
152
+ "default": "[]",
153
+ "fieldName": "selected"
154
+ }
155
+ ],
156
+ "superclass": {
157
+ "name": "LitVirtualizer",
158
+ "package": "@lit-labs/virtualizer"
159
+ },
160
+ "tagName": "sp-grid",
161
+ "customElement": true
162
+ }
163
+ ],
164
+ "exports": [
165
+ {
166
+ "kind": "js",
167
+ "name": "Grid",
168
+ "declaration": {
169
+ "name": "Grid",
170
+ "module": "src/Grid.ts"
171
+ }
172
+ }
173
+ ]
174
+ },
175
+ {
176
+ "kind": "javascript-module",
177
+ "path": "src/GridController.ts",
178
+ "declarations": [
179
+ {
180
+ "kind": "class",
181
+ "description": "",
182
+ "name": "GridController",
183
+ "members": [
184
+ {
185
+ "kind": "field",
186
+ "name": "host",
187
+ "type": {
188
+ "text": "LitVirtualizer"
189
+ },
190
+ "default": "host"
191
+ },
192
+ {
193
+ "kind": "field",
194
+ "name": "resizeController",
195
+ "type": {
196
+ "text": "ResizeController"
197
+ },
198
+ "default": "new ResizeController(this.host, {\n callback: (entries: ResizeObserverEntry[]): void => {\n entries.forEach((entry) => {\n this.measureDirectionLength(entry.contentRect);\n });\n },\n })"
199
+ },
200
+ {
201
+ "kind": "field",
202
+ "name": "rovingTabindexController",
203
+ "type": {
204
+ "text": "RovingTabindexController<T>"
205
+ },
206
+ "default": "new RovingTabindexController<T>(\n this.host,\n {\n direction: 'grid',\n elements,\n focusInIndex: () => {\n const activeElement = (this.host.getRootNode() as Document)\n .activeElement as HTMLElement;\n return activeElement === this.host ? 0 : -1;\n },\n }\n )"
207
+ },
208
+ {
209
+ "kind": "field",
210
+ "name": "itemSize",
211
+ "type": {
212
+ "text": "ItemSize"
213
+ }
214
+ },
215
+ {
216
+ "kind": "method",
217
+ "name": "_itemSize",
218
+ "privacy": "private",
219
+ "return": {
220
+ "type": {
221
+ "text": "ItemSize"
222
+ }
223
+ }
224
+ },
225
+ {
226
+ "kind": "field",
227
+ "name": "_first",
228
+ "type": {
229
+ "text": "number"
230
+ },
231
+ "default": "0"
232
+ },
233
+ {
234
+ "kind": "field",
235
+ "name": "gap",
236
+ "type": {
237
+ "text": "string | undefined"
238
+ }
239
+ },
240
+ {
241
+ "kind": "method",
242
+ "name": "_gap",
243
+ "privacy": "private",
244
+ "return": {
245
+ "type": {
246
+ "text": "string | undefined"
247
+ }
248
+ }
249
+ },
250
+ {
251
+ "kind": "field",
252
+ "name": "_last",
253
+ "type": {
254
+ "text": "number"
255
+ },
256
+ "default": "0"
257
+ },
258
+ {
259
+ "kind": "method",
260
+ "name": "focus",
261
+ "privacy": "public",
262
+ "return": {
263
+ "type": {
264
+ "text": "void"
265
+ }
266
+ },
267
+ "parameters": [
268
+ {
269
+ "name": "options",
270
+ "optional": true,
271
+ "type": {
272
+ "text": "FocusOptions"
273
+ }
274
+ }
275
+ ]
276
+ },
277
+ {
278
+ "kind": "method",
279
+ "name": "applyLayout",
280
+ "privacy": "protected",
281
+ "return": {
282
+ "type": {
283
+ "text": "void"
284
+ }
285
+ },
286
+ "parameters": [
287
+ {
288
+ "name": "itemSize",
289
+ "type": {
290
+ "text": "ItemSize | (() => ItemSize)"
291
+ }
292
+ },
293
+ {
294
+ "name": "gap",
295
+ "optional": true,
296
+ "type": {
297
+ "text": "string | (() => string)"
298
+ }
299
+ }
300
+ ]
301
+ },
302
+ {
303
+ "kind": "method",
304
+ "name": "measureDirectionLength",
305
+ "privacy": "protected",
306
+ "return": {
307
+ "type": {
308
+ "text": "void"
309
+ }
310
+ },
311
+ "parameters": [
312
+ {
313
+ "name": "contentRect",
314
+ "type": {
315
+ "text": "DOMRect"
316
+ }
317
+ }
318
+ ]
319
+ },
320
+ {
321
+ "kind": "field",
322
+ "name": "handleFocusin",
323
+ "privacy": "protected"
324
+ },
325
+ {
326
+ "kind": "field",
327
+ "name": "handleFocusout",
328
+ "privacy": "protected"
329
+ },
330
+ {
331
+ "kind": "field",
332
+ "name": "handleRangeChanged",
333
+ "privacy": "protected"
334
+ },
335
+ {
336
+ "kind": "field",
337
+ "name": "handleVisibleChanged",
338
+ "privacy": "protected"
339
+ },
340
+ {
341
+ "kind": "method",
342
+ "name": "hostConnected",
343
+ "privacy": "public",
344
+ "return": {
345
+ "type": {
346
+ "text": "void"
347
+ }
348
+ }
349
+ },
350
+ {
351
+ "kind": "method",
352
+ "name": "hostDisconnected",
353
+ "privacy": "public",
354
+ "return": {
355
+ "type": {
356
+ "text": "void"
357
+ }
358
+ }
359
+ }
360
+ ]
361
+ }
362
+ ],
363
+ "exports": [
364
+ {
365
+ "kind": "js",
366
+ "name": "GridController",
367
+ "declaration": {
368
+ "name": "GridController",
369
+ "module": "src/GridController.ts"
370
+ }
371
+ }
372
+ ]
373
+ },
374
+ {
375
+ "kind": "javascript-module",
376
+ "path": "src/grid.css.ts",
377
+ "declarations": [
378
+ {
379
+ "kind": "variable",
380
+ "name": "styles",
381
+ "default": "css`\n:host{contain:strict;display:block;position:relative}\n`"
382
+ }
383
+ ],
384
+ "exports": [
385
+ {
386
+ "kind": "js",
387
+ "name": "default",
388
+ "declaration": {
389
+ "name": "styles",
390
+ "module": "src/grid.css.ts"
391
+ }
392
+ }
393
+ ]
394
+ },
395
+ {
396
+ "kind": "javascript-module",
397
+ "path": "src/index.ts",
398
+ "declarations": [],
399
+ "exports": [
400
+ {
401
+ "kind": "js",
402
+ "name": "*",
403
+ "declaration": {
404
+ "name": "*",
405
+ "package": "./Grid.js"
406
+ }
407
+ }
408
+ ]
409
+ }
410
+ ]
411
+ }
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "@spectrum-web-components/grid",
3
+ "version": "0.0.2",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "description": "Web component implementation of a Spectrum design Grid",
8
+ "license": "Apache-2.0",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/adobe/spectrum-web-components.git",
12
+ "directory": "packages/grid"
13
+ },
14
+ "author": "",
15
+ "homepage": "https://adobe.github.io/spectrum-web-components/components/grid",
16
+ "bugs": {
17
+ "url": "https://github.com/adobe/spectrum-web-components/issues"
18
+ },
19
+ "main": "src/index.js",
20
+ "module": "src/index.js",
21
+ "type": "module",
22
+ "exports": {
23
+ ".": "./src/index.js",
24
+ "./src/*": "./src/*.js",
25
+ "./package.json": "./package.json",
26
+ "./sp-grid": "./sp-grid.js",
27
+ "./sp-grid.js": "./sp-grid.js"
28
+ },
29
+ "scripts": {
30
+ "test": "echo \"Error: run tests from mono-repo root.\" && exit 1"
31
+ },
32
+ "files": [
33
+ "**/*.d.ts",
34
+ "**/*.js",
35
+ "**/*.js.map",
36
+ "custom-elements.json",
37
+ "!stories/",
38
+ "!test/"
39
+ ],
40
+ "keywords": [
41
+ "spectrum css",
42
+ "web components",
43
+ "lit-element",
44
+ "lit-html"
45
+ ],
46
+ "dependencies": {
47
+ "@lit-labs/observers": "^1.0.1",
48
+ "@lit-labs/virtualizer": "0.7.0-pre.2",
49
+ "@spectrum-web-components/base": "^0.5.3",
50
+ "@spectrum-web-components/reactive-controllers": "^0.2.1",
51
+ "tslib": "^2.0.0"
52
+ },
53
+ "types": "./src/index.d.ts",
54
+ "customElements": "custom-elements.json",
55
+ "sideEffects": [
56
+ "./sp-*.js"
57
+ ],
58
+ "gitHead": "57aba8030b6af96af4015a0aa830e342a17dc219"
59
+ }
package/sp-grid.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { Grid } from './src/Grid.js';
2
+ declare global {
3
+ interface HTMLElementTagNameMap {
4
+ 'sp-grid': Grid;
5
+ }
6
+ }
package/sp-grid.js ADDED
@@ -0,0 +1,14 @@
1
+ /*
2
+ Copyright 2020 Adobe. All rights reserved.
3
+ This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License. You may obtain a copy
5
+ of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software distributed under
8
+ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ OF ANY KIND, either express or implied. See the License for the specific language
10
+ governing permissions and limitations under the License.
11
+ */
12
+ import { Grid } from './src/Grid.js';
13
+ customElements.define('sp-grid', Grid);
14
+ //# sourceMappingURL=sp-grid.js.map
package/sp-grid.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sp-grid.js","sourceRoot":"","sources":["sp-grid.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;EAUE;AAEF,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAErC,cAAc,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC","sourcesContent":["/*\nCopyright 2020 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nimport { Grid } from './src/Grid.js';\n\ncustomElements.define('sp-grid', Grid);\n\ndeclare global {\n interface HTMLElementTagNameMap {\n 'sp-grid': Grid;\n }\n}\n"]}
package/src/Grid.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ import { CSSResultArray, PropertyValues, TemplateResult } from '@spectrum-web-components/base';
2
+ import { LitVirtualizer } from '@lit-labs/virtualizer';
3
+ import { GridController } from './GridController.js';
4
+ /**
5
+ * @element sp-grid
6
+ *
7
+ * @fires change - Announces that the value of `selected` has changed
8
+ */
9
+ export declare class Grid extends LitVirtualizer {
10
+ static get styles(): CSSResultArray;
11
+ focusableSelector: string;
12
+ gap: `${'0' | `${number}px`}`;
13
+ items: Record<string, unknown>[];
14
+ itemSize: {
15
+ width: number;
16
+ height: number;
17
+ };
18
+ get renderItem(): (item: unknown, index: number) => TemplateResult;
19
+ set renderItem(fn: (item: unknown, index: number, selected: boolean) => TemplateResult);
20
+ selected: Record<string, unknown>[];
21
+ gridController: GridController<HTMLElement>;
22
+ protected handleChange(event: Event): void;
23
+ protected update(changes: PropertyValues<this>): void;
24
+ connectedCallback(): void;
25
+ disconnectedCallback(): void;
26
+ }
package/src/Grid.js ADDED
@@ -0,0 +1,117 @@
1
+ /*
2
+ Copyright 2020 Adobe. All rights reserved.
3
+ This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License. You may obtain a copy
5
+ of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software distributed under
8
+ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ OF ANY KIND, either express or implied. See the License for the specific language
10
+ governing permissions and limitations under the License.
11
+ */
12
+ import { __decorate } from "tslib";
13
+ import { property } from '@spectrum-web-components/base/src/decorators.js';
14
+ import { LitVirtualizer } from '@lit-labs/virtualizer';
15
+ import { grid } from '@lit-labs/virtualizer/layouts/grid.js';
16
+ import styles from './grid.css.js';
17
+ import { GridController } from './GridController.js';
18
+ /**
19
+ * @element sp-grid
20
+ *
21
+ * @fires change - Announces that the value of `selected` has changed
22
+ */
23
+ export class Grid extends LitVirtualizer {
24
+ constructor() {
25
+ super(...arguments);
26
+ this.gap = '0';
27
+ this.items = [];
28
+ this.itemSize = {
29
+ width: 200,
30
+ height: 200,
31
+ };
32
+ this.selected = [];
33
+ this.gridController = new GridController(this, {
34
+ elements: () => [],
35
+ itemSize: () => this.itemSize,
36
+ /* c8 ignore next 1 */
37
+ gap: () => this.gap,
38
+ });
39
+ }
40
+ static get styles() {
41
+ return [styles];
42
+ }
43
+ /* c8 ignore next 3 */
44
+ get renderItem() {
45
+ return super.renderItem;
46
+ }
47
+ set renderItem(fn) {
48
+ super.renderItem = (item, index) => {
49
+ const selected = this.selected.includes(item);
50
+ return fn(item, index, selected);
51
+ };
52
+ }
53
+ handleChange(event) {
54
+ const target = event.target;
55
+ const value = this.items[parseFloat(target.getAttribute('key') || '')];
56
+ const selected = [...this.selected];
57
+ if (!selected.includes(value)) {
58
+ selected.push(value);
59
+ }
60
+ else {
61
+ const index = selected.indexOf(value);
62
+ if (index > -1) {
63
+ selected.splice(index, 1);
64
+ }
65
+ }
66
+ this.selected = selected;
67
+ }
68
+ update(changes) {
69
+ if (changes.has('itemSize') ||
70
+ changes.has('gap') ||
71
+ changes.has('focusableSelector')) {
72
+ this.updateComplete.then(() => {
73
+ this.gridController.update({
74
+ elements: () => [
75
+ ...this.querySelectorAll(this.focusableSelector),
76
+ ],
77
+ itemSize: () => this.itemSize,
78
+ gap: () => this.gap,
79
+ });
80
+ });
81
+ this.layout = grid({
82
+ itemSize: {
83
+ width: `${this.itemSize.width}px`,
84
+ height: `${this.itemSize.height}px`,
85
+ },
86
+ gap: this.gap,
87
+ });
88
+ }
89
+ super.update(changes);
90
+ }
91
+ connectedCallback() {
92
+ super.connectedCallback();
93
+ this.addEventListener('change', this.handleChange, { capture: true });
94
+ }
95
+ disconnectedCallback() {
96
+ this.removeEventListener('change', this.handleChange, {
97
+ capture: true,
98
+ });
99
+ super.disconnectedCallback();
100
+ }
101
+ }
102
+ __decorate([
103
+ property({ type: String })
104
+ ], Grid.prototype, "focusableSelector", void 0);
105
+ __decorate([
106
+ property({ type: String })
107
+ ], Grid.prototype, "gap", void 0);
108
+ __decorate([
109
+ property({ type: Array })
110
+ ], Grid.prototype, "items", void 0);
111
+ __decorate([
112
+ property({ type: Object })
113
+ ], Grid.prototype, "itemSize", void 0);
114
+ __decorate([
115
+ property({ type: Array })
116
+ ], Grid.prototype, "selected", void 0);
117
+ //# sourceMappingURL=Grid.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Grid.js","sourceRoot":"","sources":["Grid.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;EAUE;;AAOF,OAAO,EAAE,QAAQ,EAAE,MAAM,iDAAiD,CAAC;AAC3E,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,IAAI,EAAE,MAAM,uCAAuC,CAAC;AAE7D,OAAO,MAAM,MAAM,eAAe,CAAC;AACnC,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAErD;;;;GAIG;AACH,MAAM,OAAO,IAAK,SAAQ,cAAc;IAAxC;;QASW,QAAG,GAA6B,GAAG,CAAC;QAGpC,UAAK,GAA8B,EAAE,CAAC;QAGtC,aAAQ,GAGX;YACA,KAAK,EAAE,GAAG;YACV,MAAM,EAAE,GAAG;SACd,CAAC;QAoBK,aAAQ,GAA8B,EAAE,CAAC;QAEhD,mBAAc,GAAG,IAAI,cAAc,CAAc,IAAI,EAAE;YACnD,QAAQ,EAAE,GAAG,EAAE,CAAC,EAAE;YAClB,QAAQ,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,QAAQ;YAC7B,sBAAsB;YACtB,GAAG,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG;SACtB,CAAC,CAAC;IA4DP,CAAC;IA3GU,MAAM,KAAK,MAAM;QACpB,OAAO,CAAC,MAAM,CAAC,CAAC;IACpB,CAAC;IAoBD,sBAAsB;IACtB,IAAI,UAAU;QACV,OAAO,KAAK,CAAC,UAAU,CAAC;IAC5B,CAAC;IAED,IAAI,UAAU,CACV,EAAuE;QAEvE,KAAK,CAAC,UAAU,GAAG,CACf,IAA6B,EAC7B,KAAa,EACC,EAAE;YAChB,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAA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;iBACtB,CAAC,CAAC;YACP,CAAC,CAAC,CAAC;YAEH,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;gBACf,QAAQ,EAAE;oBACN,KAAK,EAAE,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,IAAI;oBACjC,MAAM,EAAE,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,IAAI;iBACtC;gBACD,GAAG,EAAE,IAAI,CAAC,GAAG;aAChB,CAAC,CAAC;SACN;QAED,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAC1B,CAAC;IAED,iBAAiB;QACb,KAAK,CAAC,iBAAiB,EAAE,CAAC;QAC1B,IAAI,CAAC,gBAAgB,CAAC,QAAQ,EAAE,IAAI,CAAC,YAAY,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;IAC1E,CAAC;IAED,oBAAoB;QAChB,IAAI,CAAC,mBAAmB,CAAC,QAAQ,EAAE,IAAI,CAAC,YAAY,EAAE;YAClD,OAAO,EAAE,IAAI;SAChB,CAAC,CAAC;QACH,KAAK,CAAC,oBAAoB,EAAE,CAAC;IACjC,CAAC;CACJ;AAtGG;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;+CACO;AAGlC;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;iCACgB;AAG3C;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;mCACmB;AAG7C;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;sCAOzB;AAoBF;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;sCACsB","sourcesContent":["/*\nCopyright 2020 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nimport {\n CSSResultArray,\n PropertyValues,\n TemplateResult,\n} from '@spectrum-web-components/base';\nimport { property } from '@spectrum-web-components/base/src/decorators.js';\nimport { LitVirtualizer } from '@lit-labs/virtualizer';\nimport { grid } from '@lit-labs/virtualizer/layouts/grid.js';\n\nimport styles from './grid.css.js';\nimport { GridController } from './GridController.js';\n\n/**\n * @element sp-grid\n *\n * @fires change - Announces that the value of `selected` has changed\n */\nexport class Grid extends LitVirtualizer {\n public static get styles(): CSSResultArray {\n return [styles];\n }\n\n @property({ type: String })\n public focusableSelector!: string;\n\n @property({ type: String })\n public gap: `${'0' | `${number}px`}` = '0';\n\n @property({ type: Array })\n public items: Record<string, unknown>[] = [];\n\n @property({ type: Object })\n public itemSize: {\n width: number;\n height: number;\n } = {\n width: 200,\n height: 200,\n };\n\n /* c8 ignore next 3 */\n get renderItem(): (item: unknown, index: number) => TemplateResult {\n return super.renderItem;\n }\n\n set renderItem(\n fn: (item: unknown, index: number, selected: boolean) => TemplateResult\n ) {\n super.renderItem = (\n item: Record<string, unknown>,\n index: number\n ): TemplateResult => {\n const selected = this.selected.includes(item);\n return fn(item, index, selected);\n };\n }\n\n @property({ type: Array })\n public selected: Record<string, unknown>[] = [];\n\n gridController = new GridController<HTMLElement>(this, {\n elements: () => [],\n itemSize: () => this.itemSize,\n /* c8 ignore next 1 */\n gap: () => this.gap,\n });\n\n protected handleChange(event: Event): void {\n const target = event.target as HTMLElement;\n const value = this.items[\n parseFloat(target.getAttribute('key') || '')\n ] as Record<string, unknown>;\n const selected: Record<string, unknown>[] = [...this.selected];\n if (!selected.includes(value)) {\n selected.push(value);\n } else {\n const index = selected.indexOf(value);\n if (index > -1) {\n selected.splice(index, 1);\n }\n }\n this.selected = selected;\n }\n\n protected update(changes: PropertyValues<this>): void {\n if (\n changes.has('itemSize') ||\n changes.has('gap') ||\n changes.has('focusableSelector')\n ) {\n this.updateComplete.then(() => {\n this.gridController.update({\n elements: () => [\n ...this.querySelectorAll<HTMLElement>(\n this.focusableSelector\n ),\n ],\n itemSize: () => this.itemSize,\n gap: () => this.gap,\n });\n });\n\n this.layout = grid({\n itemSize: {\n width: `${this.itemSize.width}px`,\n height: `${this.itemSize.height}px`,\n },\n gap: this.gap,\n });\n }\n\n super.update(changes);\n }\n\n connectedCallback(): void {\n super.connectedCallback();\n this.addEventListener('change', this.handleChange, { capture: true });\n }\n\n disconnectedCallback(): void {\n this.removeEventListener('change', this.handleChange, {\n capture: true,\n });\n super.disconnectedCallback();\n }\n}\n"]}
@@ -0,0 +1,40 @@
1
+ import type { ReactiveController } from 'lit';
2
+ import { ResizeController } from '@lit-labs/observers/resize_controller.js';
3
+ import { RovingTabindexController } from '@spectrum-web-components/reactive-controllers/src/RovingTabindex.js';
4
+ import { RangeChangedEvent, VisibilityChangedEvent } from '@lit-labs/virtualizer/Virtualizer.js';
5
+ import type { LitVirtualizer } from '@lit-labs/virtualizer';
6
+ interface ItemSize {
7
+ width: number;
8
+ height: number;
9
+ }
10
+ export declare class GridController<T extends HTMLElement> implements ReactiveController {
11
+ host: LitVirtualizer;
12
+ resizeController: ResizeController;
13
+ rovingTabindexController: RovingTabindexController<T>;
14
+ get itemSize(): ItemSize;
15
+ private _itemSize;
16
+ _first: number;
17
+ get gap(): string | undefined;
18
+ private _gap;
19
+ _last: number;
20
+ constructor(host: LitVirtualizer, { elements, itemSize, gap, }: {
21
+ elements: () => T[];
22
+ itemSize: ItemSize | (() => ItemSize);
23
+ gap?: string | (() => string);
24
+ });
25
+ focus(options?: FocusOptions): void;
26
+ protected applyLayout(itemSize: ItemSize | (() => ItemSize), gap?: string | (() => string)): void;
27
+ update({ elements, itemSize, gap, }: {
28
+ elements: () => T[];
29
+ itemSize: ItemSize | (() => ItemSize);
30
+ gap?: string | (() => string);
31
+ }): void;
32
+ protected measureDirectionLength(contentRect: DOMRect): void;
33
+ protected handleFocusin: (event: FocusEvent) => void;
34
+ protected handleFocusout: (event: FocusEvent) => void;
35
+ protected handleRangeChanged: (event: RangeChangedEvent) => void;
36
+ protected handleVisibleChanged: (event: VisibilityChangedEvent) => void;
37
+ hostConnected(): void;
38
+ hostDisconnected(): void;
39
+ }
40
+ export {};
@@ -0,0 +1,129 @@
1
+ import { ResizeController } from '@lit-labs/observers/resize_controller.js';
2
+ import { RovingTabindexController } from '@spectrum-web-components/reactive-controllers/src/RovingTabindex.js';
3
+ export class GridController {
4
+ constructor(host, { elements, itemSize, gap, }) {
5
+ // First visible element
6
+ this._first = 0;
7
+ // Last visible element
8
+ this._last = 0;
9
+ this.handleFocusin = (event) => {
10
+ const doCallbackAfterPaint = (cb) => {
11
+ requestAnimationFrame(() => {
12
+ requestAnimationFrame(() => {
13
+ cb();
14
+ });
15
+ });
16
+ };
17
+ const scrollToFirst = () => this.host.scrollToIndex(0);
18
+ const focusIntoGrid = () => {
19
+ this.focus();
20
+ this.host.tabIndex = -1;
21
+ };
22
+ if (event.target === this.host) {
23
+ if (this._first > 0) {
24
+ doCallbackAfterPaint(() => {
25
+ scrollToFirst();
26
+ doCallbackAfterPaint(focusIntoGrid);
27
+ });
28
+ }
29
+ else {
30
+ doCallbackAfterPaint(focusIntoGrid);
31
+ }
32
+ }
33
+ };
34
+ this.handleFocusout = (event) => {
35
+ if (event.relatedTarget &&
36
+ !this.host.contains(event.relatedTarget)) {
37
+ this.host.tabIndex = 0;
38
+ }
39
+ };
40
+ this.handleRangeChanged = (event) => {
41
+ this.rovingTabindexController.clearElementCache(event.first);
42
+ };
43
+ this.handleVisibleChanged = (event) => {
44
+ this._first = event.first;
45
+ this._last = event.last;
46
+ };
47
+ this.host = host;
48
+ this.host.addController(this);
49
+ this.applyLayout(itemSize, gap);
50
+ this.resizeController = new ResizeController(this.host, {
51
+ callback: (entries) => {
52
+ entries.forEach((entry) => {
53
+ this.measureDirectionLength(entry.contentRect);
54
+ });
55
+ },
56
+ });
57
+ this.rovingTabindexController = new RovingTabindexController(this.host, {
58
+ direction: 'grid',
59
+ elements,
60
+ focusInIndex: () => {
61
+ const activeElement = this.host.getRootNode()
62
+ .activeElement;
63
+ return activeElement === this.host ? 0 : -1;
64
+ },
65
+ });
66
+ }
67
+ get itemSize() {
68
+ return this._itemSize();
69
+ }
70
+ /* c8 ignore next 6 */
71
+ _itemSize() {
72
+ return {
73
+ width: 100,
74
+ height: 100,
75
+ };
76
+ }
77
+ get gap() {
78
+ return this._gap();
79
+ }
80
+ /* c8 ignore next 3 */
81
+ _gap() {
82
+ return undefined;
83
+ }
84
+ focus(options) {
85
+ this.rovingTabindexController.focus(options);
86
+ }
87
+ applyLayout(itemSize, gap) {
88
+ /* c8 ignore next 2 */
89
+ if (typeof itemSize === 'object') {
90
+ this._itemSize = () => itemSize;
91
+ }
92
+ else if (typeof itemSize === 'function' &&
93
+ typeof itemSize() !== 'undefined') {
94
+ this._itemSize = itemSize;
95
+ }
96
+ /* c8 ignore next 2 */
97
+ if (typeof gap === 'string') {
98
+ this._gap = () => gap;
99
+ }
100
+ else if (typeof gap === 'function') {
101
+ this._gap = gap;
102
+ }
103
+ }
104
+ update({ elements, itemSize, gap, }) {
105
+ this.rovingTabindexController.update({ elements });
106
+ this.applyLayout(itemSize, gap);
107
+ const contentRect = this.host.getBoundingClientRect();
108
+ this.measureDirectionLength(contentRect);
109
+ }
110
+ measureDirectionLength(contentRect) {
111
+ const gap = this.gap ? parseFloat(this.gap) : 0;
112
+ this.rovingTabindexController.directionLength = Math.floor((contentRect.width - gap) / (this.itemSize.width + gap));
113
+ }
114
+ hostConnected() {
115
+ this.host.addEventListener('rangeChanged', this.handleRangeChanged);
116
+ this.host.addEventListener('visibilityChanged', this.handleVisibleChanged);
117
+ this.host.addEventListener('focusin', this.handleFocusin);
118
+ this.host.addEventListener('focusout', this.handleFocusout);
119
+ this.host.tabIndex = 0;
120
+ this.host.style.setProperty('outline', 'none', 'important');
121
+ }
122
+ hostDisconnected() {
123
+ this.host.removeEventListener('rangeChanged', this.handleRangeChanged);
124
+ this.host.removeEventListener('visibilityChanged', this.handleVisibleChanged);
125
+ this.host.removeEventListener('focusin', this.handleFocusin);
126
+ this.host.removeEventListener('focusout', this.handleFocusout);
127
+ }
128
+ }
129
+ //# sourceMappingURL=GridController.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GridController.js","sourceRoot":"","sources":["GridController.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,gBAAgB,EAAE,MAAM,0CAA0C,CAAC;AAC5E,OAAO,EAAE,wBAAwB,EAAE,MAAM,qEAAqE,CAAC;AAY/G,MAAM,OAAO,cAAc;IAoCvB,YACI,IAAoB,EACpB,EACI,QAAQ,EACR,QAAQ,EACR,GAAG,GAKN;QAzBL,wBAAwB;QACxB,WAAM,GAAG,CAAC,CAAC;QAWX,uBAAuB;QACvB,UAAK,GAAG,CAAC,CAAC;QAqFA,kBAAa,GAAG,CAAC,KAAiB,EAAQ,EAAE;YAClD,MAAM,oBAAoB,GAAG,CAAC,EAAc,EAAQ,EAAE;gBAClD,qBAAqB,CAAC,GAAG,EAAE;oBACvB,qBAAqB,CAAC,GAAG,EAAE;wBACvB,EAAE,EAAE,CAAC;oBACT,CAAC,CAAC,CAAC;gBACP,CAAC,CAAC,CAAC;YACP,CAAC,CAAC;YACF,MAAM,aAAa,GAAG,GAAS,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;YAC7D,MAAM,aAAa,GAAG,GAAS,EAAE;gBAC7B,IAAI,CAAC,KAAK,EAAE,CAAC;gBACb,IAAI,CAAC,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC;YAC5B,CAAC,CAAC;YACF,IAAK,KAAK,CAAC,MAAsB,KAAK,IAAI,CAAC,IAAI,EAAE;gBAC7C,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE;oBACjB,oBAAoB,CAAC,GAAG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,CAAC;IAEM,gBAAgB;QACnB,IAAI,CAAC,IAAI,CAAC,mBAAmB,CAAC,cAAc,EAAE,IAAI,CAAC,kBAAkB,CAAC,CAAC;QACvE,IAAI,CAAC,IAAI,CAAC,mBAAmB,CACzB,mBAAmB,EACnB,IAAI,CAAC,oBAAoB,CAC5B,CAAC;QACF,IAAI,CAAC,IAAI,CAAC,mBAAmB,CAAC,SAAS,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC;QAC7D,IAAI,CAAC,IAAI,CAAC,mBAAmB,CAAC,UAAU,EAAE,IAAI,CAAC,cAAc,CAAC,CAAC;IACnE,CAAC;CACJ","sourcesContent":["/*\nCopyright 2020 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\nimport type { ReactiveController } from 'lit';\n\nimport { ResizeController } from '@lit-labs/observers/resize_controller.js';\nimport { RovingTabindexController } from '@spectrum-web-components/reactive-controllers/src/RovingTabindex.js';\nimport {\n RangeChangedEvent,\n VisibilityChangedEvent,\n} from '@lit-labs/virtualizer/Virtualizer.js';\nimport type { LitVirtualizer } from '@lit-labs/virtualizer';\n\ninterface ItemSize {\n width: number;\n height: number;\n}\n\nexport class GridController<T extends HTMLElement>\n implements ReactiveController\n{\n host!: LitVirtualizer;\n\n resizeController!: ResizeController;\n\n rovingTabindexController!: RovingTabindexController<T>;\n\n get itemSize(): ItemSize {\n return this._itemSize();\n }\n\n /* c8 ignore next 6 */\n private _itemSize(): ItemSize {\n return {\n width: 100,\n height: 100,\n };\n }\n\n // First visible element\n _first = 0;\n\n get gap(): string | undefined {\n return this._gap();\n }\n\n /* c8 ignore next 3 */\n private _gap(): string | undefined {\n return undefined;\n }\n\n // Last visible element\n _last = 0;\n\n constructor(\n host: LitVirtualizer,\n {\n elements,\n itemSize,\n gap,\n }: {\n elements: () => T[];\n itemSize: ItemSize | (() => ItemSize);\n gap?: string | (() => string);\n }\n ) {\n this.host = host;\n this.host.addController(this);\n this.applyLayout(itemSize, gap);\n this.resizeController = new ResizeController(this.host, {\n callback: (entries: ResizeObserverEntry[]): void => {\n entries.forEach((entry) => {\n this.measureDirectionLength(entry.contentRect);\n });\n },\n });\n this.rovingTabindexController = new RovingTabindexController<T>(\n this.host,\n {\n direction: 'grid',\n elements,\n focusInIndex: () => {\n const activeElement = (this.host.getRootNode() as Document)\n .activeElement as HTMLElement;\n return activeElement === this.host ? 0 : -1;\n },\n }\n );\n }\n\n public focus(options?: FocusOptions): void {\n this.rovingTabindexController.focus(options);\n }\n\n protected applyLayout(\n itemSize: ItemSize | (() => ItemSize),\n gap?: string | (() => string)\n ): void {\n /* c8 ignore next 2 */\n if (typeof itemSize === 'object') {\n this._itemSize = () => itemSize;\n } else if (\n typeof itemSize === 'function' &&\n typeof itemSize() !== 'undefined'\n ) {\n this._itemSize = itemSize;\n }\n /* c8 ignore next 2 */\n if (typeof gap === 'string') {\n this._gap = () => gap;\n } else if (typeof gap === 'function') {\n this._gap = gap;\n }\n }\n\n public update({\n elements,\n itemSize,\n gap,\n }: {\n elements: () => T[];\n itemSize: ItemSize | (() => ItemSize);\n gap?: string | (() => string);\n }): void {\n this.rovingTabindexController.update({ elements });\n this.applyLayout(itemSize, gap);\n const contentRect = this.host.getBoundingClientRect();\n this.measureDirectionLength(contentRect);\n }\n\n protected measureDirectionLength(contentRect: DOMRect): void {\n const gap = this.gap ? parseFloat(this.gap) : 0;\n this.rovingTabindexController.directionLength = Math.floor(\n (contentRect.width - gap) / (this.itemSize.width + gap)\n );\n }\n\n protected handleFocusin = (event: FocusEvent): void => {\n const doCallbackAfterPaint = (cb: () => void): void => {\n requestAnimationFrame(() => {\n requestAnimationFrame(() => {\n cb();\n });\n });\n };\n const scrollToFirst = (): void => this.host.scrollToIndex(0);\n const focusIntoGrid = (): void => {\n this.focus();\n this.host.tabIndex = -1;\n };\n if ((event.target as HTMLElement) === this.host) {\n if (this._first > 0) {\n doCallbackAfterPaint(() => {\n scrollToFirst();\n doCallbackAfterPaint(focusIntoGrid);\n });\n } else {\n doCallbackAfterPaint(focusIntoGrid);\n }\n }\n };\n\n protected handleFocusout = (event: FocusEvent): void => {\n if (\n event.relatedTarget &&\n !this.host.contains(event.relatedTarget as HTMLElement)\n ) {\n this.host.tabIndex = 0;\n }\n };\n\n protected handleRangeChanged = (event: RangeChangedEvent): void => {\n this.rovingTabindexController.clearElementCache(event.first);\n };\n\n protected handleVisibleChanged = (event: VisibilityChangedEvent): void => {\n this._first = event.first;\n this._last = event.last;\n };\n\n public hostConnected(): void {\n this.host.addEventListener('rangeChanged', this.handleRangeChanged);\n this.host.addEventListener(\n 'visibilityChanged',\n this.handleVisibleChanged\n );\n this.host.addEventListener('focusin', this.handleFocusin);\n this.host.addEventListener('focusout', this.handleFocusout);\n this.host.tabIndex = 0;\n this.host.style.setProperty('outline', 'none', 'important');\n }\n\n public hostDisconnected(): void {\n this.host.removeEventListener('rangeChanged', this.handleRangeChanged);\n this.host.removeEventListener(\n 'visibilityChanged',\n this.handleVisibleChanged\n );\n this.host.removeEventListener('focusin', this.handleFocusin);\n this.host.removeEventListener('focusout', this.handleFocusout);\n }\n}\n"]}
@@ -0,0 +1,2 @@
1
+ declare const styles: import("@spectrum-web-components/base").CSSResult;
2
+ export default styles;
@@ -0,0 +1,17 @@
1
+ /*
2
+ Copyright 2020 Adobe. All rights reserved.
3
+ This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License. You may obtain a copy
5
+ of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software distributed under
8
+ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ OF ANY KIND, either express or implied. See the License for the specific language
10
+ governing permissions and limitations under the License.
11
+ */
12
+ import { css } from '@spectrum-web-components/base';
13
+ const styles = css `
14
+ :host{contain:strict;display:block;position:relative}
15
+ `;
16
+ export default styles;
17
+ //# sourceMappingURL=grid.css.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grid.css.js","sourceRoot":"","sources":["grid.css.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;EAUE;AACF,OAAO,EAAE,GAAG,EAAE,MAAM,+BAA+B,CAAC;AACpD,MAAM,MAAM,GAAG,GAAG,CAAA;;CAEjB,CAAC;AACF,eAAe,MAAM,CAAC","sourcesContent":["/*\nCopyright 2020 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\nimport { css } from '@spectrum-web-components/base';\nconst styles = css`\n:host{contain:strict;display:block;position:relative}\n`;\nexport default styles;"]}
package/src/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './Grid.js';
package/src/index.js ADDED
@@ -0,0 +1,13 @@
1
+ /*
2
+ Copyright 2020 Adobe. All rights reserved.
3
+ This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License. You may obtain a copy
5
+ of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software distributed under
8
+ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ OF ANY KIND, either express or implied. See the License for the specific language
10
+ governing permissions and limitations under the License.
11
+ */
12
+ export * from './Grid.js';
13
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;EAUE;AAEF,cAAc,WAAW,CAAC","sourcesContent":["/*\nCopyright 2020 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nexport * from './Grid.js';\n"]}