@spectrum-web-components/grid 0.41.2 → 0.42.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spectrum-web-components/grid",
3
- "version": "0.41.2",
3
+ "version": "0.42.1",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -64,8 +64,8 @@
64
64
  "dependencies": {
65
65
  "@lit-labs/observers": "^2.0.0",
66
66
  "@lit-labs/virtualizer": "^2.0.6",
67
- "@spectrum-web-components/base": "^0.41.2",
68
- "@spectrum-web-components/reactive-controllers": "^0.41.2",
67
+ "@spectrum-web-components/base": "^0.42.1",
68
+ "@spectrum-web-components/reactive-controllers": "^0.42.1",
69
69
  "lit": "^2.5.0"
70
70
  },
71
71
  "types": "./src/index.d.ts",
@@ -74,5 +74,5 @@
74
74
  "./sp-*.js",
75
75
  "./**/*.dev.js"
76
76
  ],
77
- "gitHead": "78c3f16b08c9133c9e5ca88d0c9fef5ea7d2ab87"
77
+ "gitHead": "c7ab5516e86d20194e92114afd04affa490b7248"
78
78
  }
@@ -1,6 +1,13 @@
1
1
  "use strict";
2
2
  import { ResizeController } from "@lit-labs/observers/resize-controller.js";
3
3
  import { RovingTabindexController } from "@spectrum-web-components/reactive-controllers/src/RovingTabindex.js";
4
+ const doCallbackAfterPaint = (cb) => {
5
+ requestAnimationFrame(() => {
6
+ requestAnimationFrame(() => {
7
+ cb();
8
+ });
9
+ });
10
+ };
4
11
  export class GridController {
5
12
  constructor(host, {
6
13
  elements,
@@ -13,13 +20,6 @@ export class GridController {
13
20
  // Last visible element
14
21
  this._last = 0;
15
22
  this.handleFocusin = (event) => {
16
- const doCallbackAfterPaint = (cb) => {
17
- requestAnimationFrame(() => {
18
- requestAnimationFrame(() => {
19
- cb();
20
- });
21
- });
22
- };
23
23
  const scrollToFirst = () => this.host.scrollToIndex(0);
24
24
  const focusIntoGrid = () => {
25
25
  this.focus();
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["GridController.ts"],
4
- "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/events.js';\nimport { Grid } from './Grid.dev.js'\n\ninterface ItemSize {\n width: number;\n height: number;\n}\n\nexport class GridController<T extends HTMLElement>\n implements ReactiveController\n{\n host!: Grid;\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 get padding(): string | undefined {\n return this._padding();\n }\n\n /* c8 ignore next 3 */\n private _padding(): string | undefined {\n return undefined;\n }\n\n // Last visible element\n _last = 0;\n\n constructor(\n host: Grid,\n {\n elements,\n itemSize,\n gap,\n padding,\n }: {\n elements: () => T[];\n itemSize: ItemSize | (() => ItemSize);\n gap?: string | (() => string);\n padding?: string | (() => string);\n }\n ) {\n this.host = host;\n this.host.addController(this);\n this.applyLayout(itemSize, gap, padding);\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 padding?: 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 /* c8 ignore next 2 */\n if (typeof padding === 'string') {\n this._padding = () => padding;\n } else if (typeof padding === 'function') {\n this._padding = padding;\n }\n }\n\n public update({\n elements,\n itemSize,\n gap,\n padding,\n }: {\n elements: () => T[];\n itemSize: ItemSize | (() => ItemSize);\n gap?: string | (() => string);\n padding?: string | (() => string);\n }): void {\n this.rovingTabindexController.update({ elements });\n this.applyLayout(itemSize, gap, padding);\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 const padding = this.padding ? parseFloat(this.padding) : 0;\n const contentBoxWidth = contentRect.width - padding * 2;\n // There is always one less gap per row than items.\n const directionLength =\n Math.floor(\n (contentBoxWidth - this.itemSize.width) /\n (gap + this.itemSize.width)\n ) + 1;\n this.rovingTabindexController.directionLength =\n Math.floor(directionLength);\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"],
5
- "mappings": ";AAaA,SAAS,wBAAwB;AACjC,SAAS,gCAAgC;AAYlC,aAAM,eAEb;AAAA,EA2CI,YACI,MACA;AAAA,IACI;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACJ,GAMF;AApCF;AAAA,kBAAS;AAqBT;AAAA,iBAAQ;AAuGR,SAAU,gBAAgB,CAAC,UAA4B;AACnD,YAAM,uBAAuB,CAAC,OAAyB;AACnD,8BAAsB,MAAM;AACxB,gCAAsB,MAAM;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AC9C,UAAM,UAAU,KAAK,UAAU,WAAW,KAAK,OAAO,IAAI;AAC1D,UAAM,kBAAkB,YAAY,QAAQ,UAAU;AAEtD,UAAM,kBACF,KAAK;AAAA,OACA,kBAAkB,KAAK,SAAS,UAC5B,MAAM,KAAK,SAAS;AAAA,IAC7B,IAAI;AACR,SAAK,yBAAyB,kBAC1B,KAAK,MAAM,eAAe;AAAA,EAClC;AAAA,EA6CO,gBAAsB;AACzB,SAAK,KAAK,iBAAiB,gBAAgB,KAAK,kBAAkB;AAClE,SAAK,KAAK;AAAA,MACN;AAAA,MACA,KAAK;AAAA,IACT;AACA,SAAK,KAAK,iBAAiB,WAAW,KAAK,aAAa;AACxD,SAAK,KAAK,iBAAiB,YAAY,KAAK,cAAc;AAC1D,SAAK,KAAK,WAAW;AACrB,SAAK,KAAK,MAAM,YAAY,WAAW,QAAQ,WAAW;AAAA,EAC9D;AAAA,EAEO,mBAAyB;AAC5B,SAAK,KAAK,oBAAoB,gBAAgB,KAAK,kBAAkB;AACrE,SAAK,KAAK;AAAA,MACN;AAAA,MACA,KAAK;AAAA,IACT;AACA,SAAK,KAAK,oBAAoB,WAAW,KAAK,aAAa;AAC3D,SAAK,KAAK,oBAAoB,YAAY,KAAK,cAAc;AAAA,EACjE;AACJ;",
4
+ "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/events.js';\nimport { Grid } from './Grid.dev.js'\n\ninterface ItemSize {\n width: number;\n height: number;\n}\n\nconst doCallbackAfterPaint = (cb: () => void): void => {\n requestAnimationFrame(() => {\n requestAnimationFrame(() => {\n cb();\n });\n });\n};\n\nexport class GridController<T extends HTMLElement>\n implements ReactiveController\n{\n host!: Grid;\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 get padding(): string | undefined {\n return this._padding();\n }\n\n /* c8 ignore next 3 */\n private _padding(): string | undefined {\n return undefined;\n }\n\n // Last visible element\n _last = 0;\n\n constructor(\n host: Grid,\n {\n elements,\n itemSize,\n gap,\n padding,\n }: {\n elements: () => T[];\n itemSize: ItemSize | (() => ItemSize);\n gap?: string | (() => string);\n padding?: string | (() => string);\n }\n ) {\n this.host = host;\n this.host.addController(this);\n this.applyLayout(itemSize, gap, padding);\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 padding?: 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 /* c8 ignore next 2 */\n if (typeof padding === 'string') {\n this._padding = () => padding;\n } else if (typeof padding === 'function') {\n this._padding = padding;\n }\n }\n\n public update({\n elements,\n itemSize,\n gap,\n padding,\n }: {\n elements: () => T[];\n itemSize: ItemSize | (() => ItemSize);\n gap?: string | (() => string);\n padding?: string | (() => string);\n }): void {\n this.rovingTabindexController.update({ elements });\n this.applyLayout(itemSize, gap, padding);\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 const padding = this.padding ? parseFloat(this.padding) : 0;\n const contentBoxWidth = contentRect.width - padding * 2;\n // There is always one less gap per row than items.\n const directionLength =\n Math.floor(\n (contentBoxWidth - this.itemSize.width) /\n (gap + this.itemSize.width)\n ) + 1;\n this.rovingTabindexController.directionLength =\n Math.floor(directionLength);\n }\n\n protected handleFocusin = (event: FocusEvent): void => {\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"],
5
+ "mappings": ";AAaA,SAAS,wBAAwB;AACjC,SAAS,gCAAgC;AAYzC,MAAM,uBAAuB,CAAC,OAAyB;AACnD,wBAAsB,MAAM;AACxB,0BAAsB,MAAM;AACxB,SAAG;AAAA,IACP,CAAC;AAAA,EACL,CAAC;AACL;AAEO,aAAM,eAEb;AAAA,EA2CI,YACI,MACA;AAAA,IACI;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACJ,GAMF;AApCF;AAAA,kBAAS;AAqBT;AAAA,iBAAQ;AAuGR,SAAU,gBAAgB,CAAC,UAA4B;AACnD,YAAM,gBAAgB,MAAY,KAAK,KAAK,cAAc,CAAC;AAC3D,YAAM,gBAAgB,MAAY;AAC9B,aAAK,MAAM;AACX,aAAK,KAAK,WAAW;AAAA,MACzB;AACA,UAAK,MAAM,WAA2B,KAAK,MAAM;AAC7C,YAAI,KAAK,SAAS,GAAG;AACjB,+BAAqB,MAAM;AACvB,0BAAc;AACd,iCAAqB,aAAa;AAAA,UACtC,CAAC;AAAA,QACL,OAAO;AACH,+BAAqB,aAAa;AAAA,QACtC;AAAA,MACJ;AAAA,IACJ;AAEA,SAAU,iBAAiB,CAAC,UAA4B;AACpD,UACI,CAAC,MAAM,iBACP,CAAC,KAAK,KAAK,SAAS,MAAM,aAA4B,GACxD;AACE,aAAK,KAA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,YAAY,WAAW,QAAQ,WAAW;AAAA,EAC9D;AAAA,EAEO,mBAAyB;AAC5B,SAAK,KAAK,oBAAoB,gBAAgB,KAAK,kBAAkB;AACrE,SAAK,KAAK;AAAA,MACN;AAAA,MACA,KAAK;AAAA,IACT;AACA,SAAK,KAAK,oBAAoB,WAAW,KAAK,aAAa;AAC3D,SAAK,KAAK,oBAAoB,YAAY,KAAK,cAAc;AAAA,EACjE;AACJ;",
6
6
  "names": []
7
7
  }
@@ -1,2 +1,2 @@
1
- "use strict";import{ResizeController as h}from"@lit-labs/observers/resize-controller.js";import{RovingTabindexController as d}from"@spectrum-web-components/reactive-controllers/src/RovingTabindex.js";export class GridController{constructor(t,{elements:e,itemSize:i,gap:n,padding:s}){this._first=0;this._last=0;this.handleFocusin=t=>{const e=s=>{requestAnimationFrame(()=>{requestAnimationFrame(()=>{s()})})},i=()=>this.host.scrollToIndex(0),n=()=>{this.focus(),this.host.tabIndex=-1};t.target===this.host&&(this._first>0?e(()=>{i(),e(n)}):e(n))};this.handleFocusout=t=>{(!t.relatedTarget||!this.host.contains(t.relatedTarget))&&(this.host.tabIndex=0)};this.handleRangeChanged=t=>{this.rovingTabindexController.clearElementCache(t.first)};this.handleVisibleChanged=t=>{this._first=t.first,this._last=t.last};this.host=t,this.host.addController(this),this.applyLayout(i,n,s),this.resizeController=new h(this.host,{callback:o=>{o.forEach(r=>{this.measureDirectionLength(r.contentRect)})}}),this.rovingTabindexController=new d(this.host,{direction:"grid",elements:e,focusInIndex:()=>this.host.getRootNode().activeElement===this.host?0:-1})}get itemSize(){return this._itemSize()}_itemSize(){return{width:100,height:100}}get gap(){return this._gap()}_gap(){}get padding(){return this._padding()}_padding(){}focus(t){this.rovingTabindexController.focus(t)}applyLayout(t,e,i){typeof t=="object"?this._itemSize=()=>t:typeof t=="function"&&typeof t()!="undefined"&&(this._itemSize=t),typeof e=="string"?this._gap=()=>e:typeof e=="function"&&(this._gap=e),typeof i=="string"?this._padding=()=>i:typeof i=="function"&&(this._padding=i)}update({elements:t,itemSize:e,gap:i,padding:n}){this.rovingTabindexController.update({elements:t}),this.applyLayout(e,i,n);const s=this.host.getBoundingClientRect();this.measureDirectionLength(s)}measureDirectionLength(t){const e=this.gap?parseFloat(this.gap):0,i=this.padding?parseFloat(this.padding):0,n=t.width-i*2,s=Math.floor((n-this.itemSize.width)/(e+this.itemSize.width))+1;this.rovingTabindexController.directionLength=Math.floor(s)}hostConnected(){this.host.addEventListener("rangeChanged",this.handleRangeChanged),this.host.addEventListener("visibilityChanged",this.handleVisibleChanged),this.host.addEventListener("focusin",this.handleFocusin),this.host.addEventListener("focusout",this.handleFocusout),this.host.tabIndex=0,this.host.style.setProperty("outline","none","important")}hostDisconnected(){this.host.removeEventListener("rangeChanged",this.handleRangeChanged),this.host.removeEventListener("visibilityChanged",this.handleVisibleChanged),this.host.removeEventListener("focusin",this.handleFocusin),this.host.removeEventListener("focusout",this.handleFocusout)}}
1
+ "use strict";import{ResizeController as a}from"@lit-labs/observers/resize-controller.js";import{RovingTabindexController as l}from"@spectrum-web-components/reactive-controllers/src/RovingTabindex.js";const o=r=>{requestAnimationFrame(()=>{requestAnimationFrame(()=>{r()})})};export class GridController{constructor(t,{elements:i,itemSize:e,gap:n,padding:s}){this._first=0;this._last=0;this.handleFocusin=t=>{const i=()=>this.host.scrollToIndex(0),e=()=>{this.focus(),this.host.tabIndex=-1};t.target===this.host&&(this._first>0?o(()=>{i(),o(e)}):o(e))};this.handleFocusout=t=>{(!t.relatedTarget||!this.host.contains(t.relatedTarget))&&(this.host.tabIndex=0)};this.handleRangeChanged=t=>{this.rovingTabindexController.clearElementCache(t.first)};this.handleVisibleChanged=t=>{this._first=t.first,this._last=t.last};this.host=t,this.host.addController(this),this.applyLayout(e,n,s),this.resizeController=new a(this.host,{callback:h=>{h.forEach(d=>{this.measureDirectionLength(d.contentRect)})}}),this.rovingTabindexController=new l(this.host,{direction:"grid",elements:i,focusInIndex:()=>this.host.getRootNode().activeElement===this.host?0:-1})}get itemSize(){return this._itemSize()}_itemSize(){return{width:100,height:100}}get gap(){return this._gap()}_gap(){}get padding(){return this._padding()}_padding(){}focus(t){this.rovingTabindexController.focus(t)}applyLayout(t,i,e){typeof t=="object"?this._itemSize=()=>t:typeof t=="function"&&typeof t()!="undefined"&&(this._itemSize=t),typeof i=="string"?this._gap=()=>i:typeof i=="function"&&(this._gap=i),typeof e=="string"?this._padding=()=>e:typeof e=="function"&&(this._padding=e)}update({elements:t,itemSize:i,gap:e,padding:n}){this.rovingTabindexController.update({elements:t}),this.applyLayout(i,e,n);const s=this.host.getBoundingClientRect();this.measureDirectionLength(s)}measureDirectionLength(t){const i=this.gap?parseFloat(this.gap):0,e=this.padding?parseFloat(this.padding):0,n=t.width-e*2,s=Math.floor((n-this.itemSize.width)/(i+this.itemSize.width))+1;this.rovingTabindexController.directionLength=Math.floor(s)}hostConnected(){this.host.addEventListener("rangeChanged",this.handleRangeChanged),this.host.addEventListener("visibilityChanged",this.handleVisibleChanged),this.host.addEventListener("focusin",this.handleFocusin),this.host.addEventListener("focusout",this.handleFocusout),this.host.tabIndex=0,this.host.style.setProperty("outline","none","important")}hostDisconnected(){this.host.removeEventListener("rangeChanged",this.handleRangeChanged),this.host.removeEventListener("visibilityChanged",this.handleVisibleChanged),this.host.removeEventListener("focusin",this.handleFocusin),this.host.removeEventListener("focusout",this.handleFocusout)}}
2
2
  //# sourceMappingURL=GridController.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["GridController.ts"],
4
- "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/events.js';\nimport { Grid } from './Grid.js';\n\ninterface ItemSize {\n width: number;\n height: number;\n}\n\nexport class GridController<T extends HTMLElement>\n implements ReactiveController\n{\n host!: Grid;\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 get padding(): string | undefined {\n return this._padding();\n }\n\n /* c8 ignore next 3 */\n private _padding(): string | undefined {\n return undefined;\n }\n\n // Last visible element\n _last = 0;\n\n constructor(\n host: Grid,\n {\n elements,\n itemSize,\n gap,\n padding,\n }: {\n elements: () => T[];\n itemSize: ItemSize | (() => ItemSize);\n gap?: string | (() => string);\n padding?: string | (() => string);\n }\n ) {\n this.host = host;\n this.host.addController(this);\n this.applyLayout(itemSize, gap, padding);\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 padding?: 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 /* c8 ignore next 2 */\n if (typeof padding === 'string') {\n this._padding = () => padding;\n } else if (typeof padding === 'function') {\n this._padding = padding;\n }\n }\n\n public update({\n elements,\n itemSize,\n gap,\n padding,\n }: {\n elements: () => T[];\n itemSize: ItemSize | (() => ItemSize);\n gap?: string | (() => string);\n padding?: string | (() => string);\n }): void {\n this.rovingTabindexController.update({ elements });\n this.applyLayout(itemSize, gap, padding);\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 const padding = this.padding ? parseFloat(this.padding) : 0;\n const contentBoxWidth = contentRect.width - padding * 2;\n // There is always one less gap per row than items.\n const directionLength =\n Math.floor(\n (contentBoxWidth - this.itemSize.width) /\n (gap + this.itemSize.width)\n ) + 1;\n this.rovingTabindexController.directionLength =\n Math.floor(directionLength);\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"],
5
- "mappings": "aAaA,OAAS,oBAAAA,MAAwB,2CACjC,OAAS,4BAAAC,MAAgC,sEAYlC,aAAM,cAEb,CA2CI,YACIC,EACA,CACI,SAAAC,EACA,SAAAC,EACA,IAAAC,EACA,QAAAC,CACJ,EAMF,CApCF,YAAS,EAqBT,WAAQ,EAuGR,KAAU,cAAiBC,GAA4B,CACnD,MAAMC,EAAwBC,GAAyB,CACnD,sBAAsB,IAAM,CACxB,sBAAsB,IAAM,CACxBA,EAAG,CACP,CAAC,CACL,CAAC,CACL,EACMC,EAAgB,IAAY,KAAK,KAAK,cAAc,CAAC,EACrDC,EAAgB,IAAY,CAC9B,KAAK,MAAM,EACX,KAAK,KAAK,SAAW,EACzB,EACKJ,EAAM,SAA2B,KAAK,OACnC,KAAK,OAAS,EACdC,EAAqB,IAAM,CACvBE,EAAc,EACdF,EAAqBG,CAAa,CACtC,CAAC,EAEDH,EAAqBG,CAAa,EAG9C,EAEA,KAAU,eAAkBJ,GAA4B,EAEhD,CAACA,EAAM,eACP,CAAC,KAAK,KAAK,SAASA,EAAM,aAA4B,KAEt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AAO,CACV,SAAAH,EACA,SAAAC,EACA,IAAAC,EACA,QAAAC,CACJ,EAKS,CACL,KAAK,yBAAyB,OAAO,CAAE,SAAAH,CAAS,CAAC,EACjD,KAAK,YAAYC,EAAUC,EAAKC,CAAO,EACvC,MAAMS,EAAc,KAAK,KAAK,sBAAsB,EACpD,KAAK,uBAAuBA,CAAW,CAC3C,CAEU,uBAAuBA,EAA4B,CACzD,MAAMV,EAAM,KAAK,IAAM,WAAW,KAAK,GAAG,EAAI,EACxCC,EAAU,KAAK,QAAU,WAAW,KAAK,OAAO,EAAI,EACpDU,EAAkBD,EAAY,MAAQT,EAAU,EAEhDW,EACF,KAAK,OACAD,EAAkB,KAAK,SAAS,QAC5BX,EAAM,KAAK,SAAS,MAC7B,EAAI,EACR,KAAK,yBAAyB,gBAC1B,KAAK,MAAMY,CAAe,CAClC,CA6CO,eAAsB,CACzB,KAAK,KAAK,iBAAiB,eAAgB,KAAK,kBAAkB,EAClE,KAAK,KAAK,iBACN,oBACA,KAAK,oBACT,EACA,KAAK,KAAK,iBAAiB,UAAW,KAAK,aAAa,EACxD,KAAK,KAAK,iBAAiB,WAAY,KAAK,cAAc,EAC1D,KAAK,KAAK,SAAW,EACrB,KAAK,KAAK,MAAM,YAAY,UAAW,OAAQ,WAAW,CAC9D,CAEO,kBAAyB,CAC5B,KAAK,KAAK,oBAAoB,eAAgB,KAAK,kBAAkB,EACrE,KAAK,KAAK,oBACN,oBACA,KAAK,oBACT,EACA,KAAK,KAAK,oBAAoB,UAAW,KAAK,aAAa,EAC3D,KAAK,KAAK,oBAAoB,WAAY,KAAK,cAAc,CACjE,CACJ",
6
- "names": ["ResizeController", "RovingTabindexController", "host", "elements", "itemSize", "gap", "padding", "event", "doCallbackAfterPaint", "cb", "scrollToFirst", "focusIntoGrid", "entries", "entry", "options", "contentRect", "contentBoxWidth", "directionLength"]
4
+ "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/events.js';\nimport { Grid } from './Grid.js';\n\ninterface ItemSize {\n width: number;\n height: number;\n}\n\nconst doCallbackAfterPaint = (cb: () => void): void => {\n requestAnimationFrame(() => {\n requestAnimationFrame(() => {\n cb();\n });\n });\n};\n\nexport class GridController<T extends HTMLElement>\n implements ReactiveController\n{\n host!: Grid;\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 get padding(): string | undefined {\n return this._padding();\n }\n\n /* c8 ignore next 3 */\n private _padding(): string | undefined {\n return undefined;\n }\n\n // Last visible element\n _last = 0;\n\n constructor(\n host: Grid,\n {\n elements,\n itemSize,\n gap,\n padding,\n }: {\n elements: () => T[];\n itemSize: ItemSize | (() => ItemSize);\n gap?: string | (() => string);\n padding?: string | (() => string);\n }\n ) {\n this.host = host;\n this.host.addController(this);\n this.applyLayout(itemSize, gap, padding);\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 padding?: 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 /* c8 ignore next 2 */\n if (typeof padding === 'string') {\n this._padding = () => padding;\n } else if (typeof padding === 'function') {\n this._padding = padding;\n }\n }\n\n public update({\n elements,\n itemSize,\n gap,\n padding,\n }: {\n elements: () => T[];\n itemSize: ItemSize | (() => ItemSize);\n gap?: string | (() => string);\n padding?: string | (() => string);\n }): void {\n this.rovingTabindexController.update({ elements });\n this.applyLayout(itemSize, gap, padding);\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 const padding = this.padding ? parseFloat(this.padding) : 0;\n const contentBoxWidth = contentRect.width - padding * 2;\n // There is always one less gap per row than items.\n const directionLength =\n Math.floor(\n (contentBoxWidth - this.itemSize.width) /\n (gap + this.itemSize.width)\n ) + 1;\n this.rovingTabindexController.directionLength =\n Math.floor(directionLength);\n }\n\n protected handleFocusin = (event: FocusEvent): void => {\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"],
5
+ "mappings": "aAaA,OAAS,oBAAAA,MAAwB,2CACjC,OAAS,4BAAAC,MAAgC,sEAYzC,MAAMC,EAAwBC,GAAyB,CACnD,sBAAsB,IAAM,CACxB,sBAAsB,IAAM,CACxBA,EAAG,CACP,CAAC,CACL,CAAC,CACL,EAEO,aAAM,cAEb,CA2CI,YACIC,EACA,CACI,SAAAC,EACA,SAAAC,EACA,IAAAC,EACA,QAAAC,CACJ,EAMF,CApCF,YAAS,EAqBT,WAAQ,EAuGR,KAAU,cAAiBC,GAA4B,CACnD,MAAMC,EAAgB,IAAY,KAAK,KAAK,cAAc,CAAC,EACrDC,EAAgB,IAAY,CAC9B,KAAK,MAAM,EACX,KAAK,KAAK,SAAW,EACzB,EACKF,EAAM,S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
6
+ "names": ["ResizeController", "RovingTabindexController", "doCallbackAfterPaint", "cb", "host", "elements", "itemSize", "gap", "padding", "event", "scrollToFirst", "focusIntoGrid", "entries", "entry", "options", "contentRect", "contentBoxWidth", "directionLength"]
7
7
  }
@@ -1,43 +1,6 @@
1
1
  "use strict";
2
- import { expect, fixture, nextFrame } from "@open-wc/testing";
3
- import { html, render } from "@spectrum-web-components/base";
2
+ import { fixture } from "@open-wc/testing";
4
3
  import { Default } from "../stories/grid.stories.js";
5
- import { usedHeapMB } from "../../../test/testing-helpers.js";
6
- describe("Grid memory usage", () => {
7
- it("releases references on disconnect", async function() {
8
- if (!window.gc || !("measureUserAgentSpecificMemory" in performance))
9
- this.skip();
10
- this.timeout(1e4);
11
- const iterations = 50;
12
- let active = false;
13
- const el = await fixture(
14
- html`
15
- <div></div>
16
- `
17
- );
18
- async function toggle(forced = void 0) {
19
- active = forced != null ? forced : !active;
20
- render(active ? Default() : html``, el);
21
- await nextFrame();
22
- await nextFrame();
23
- }
24
- for (let i = 0; i < 5; i++) {
25
- await toggle();
26
- }
27
- await toggle(false);
28
- const beforeMB = await usedHeapMB();
29
- for (let i = 0; i < iterations; i++) {
30
- await toggle();
31
- }
32
- await toggle(false);
33
- const afterMB = await usedHeapMB();
34
- expect(
35
- afterMB.total - beforeMB.total,
36
- `Total | before: ${beforeMB.total}, after: ${afterMB.total}
37
- DOM | before: ${beforeMB.dom}, after: ${afterMB.dom}
38
- JS | before: ${beforeMB.js}, after: ${afterMB.js}
39
- Shared | before: ${beforeMB.shared}, after: ${afterMB.shared}`
40
- ).to.be.lte(0);
41
- });
42
- });
4
+ import { testForMemoryLeaks } from "../../../test/testing-helpers.js";
5
+ testForMemoryLeaks(async () => await fixture(Default()));
43
6
  //# sourceMappingURL=grid-memory.test.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["grid-memory.test.ts"],
4
- "sourcesContent": ["/*\nCopyright 2023 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 { expect, fixture, nextFrame } from '@open-wc/testing';\nimport { html, render } from '@spectrum-web-components/base';\nimport { Default } from '../stories/grid.stories.js';\nimport { usedHeapMB } from '../../../test/testing-helpers.js';\n\ndescribe('Grid memory usage', () => {\n it('releases references on disconnect', async function () {\n if (!window.gc || !('measureUserAgentSpecificMemory' in performance))\n this.skip();\n\n this.timeout(10000);\n\n const iterations = 50;\n let active = false;\n\n const el = await fixture<HTMLElement>(\n html`\n <div></div>\n `\n );\n\n async function toggle(\n forced: boolean | undefined = undefined\n ): Promise<void> {\n active = forced != null ? forced : !active;\n render(active ? Default() : html``, el);\n await nextFrame();\n await nextFrame();\n }\n\n // \"shake things out\" to get a good first reading\n for (let i = 0; i < 5; i++) {\n await toggle();\n }\n await toggle(false);\n const beforeMB = await usedHeapMB();\n\n for (let i = 0; i < iterations; i++) {\n await toggle();\n }\n await toggle(false);\n const afterMB = await usedHeapMB();\n\n /**\n * An actually leak here shapes up to be more than 10MB per test,\n * we could be more linient later, if needed, but the test currently\n * shows less heap after the test cycle.\n */\n expect(\n afterMB.total - beforeMB.total,\n `Total | before: ${beforeMB.total}, after: ${afterMB.total}\nDOM | before: ${beforeMB.dom}, after: ${afterMB.dom}\nJS | before: ${beforeMB.js}, after: ${afterMB.js}\nShared | before: ${beforeMB.shared}, after: ${afterMB.shared}`\n ).to.be.lte(0);\n });\n});\n"],
5
- "mappings": ";AAYA,SAAS,QAAQ,SAAS,iBAAiB;AAC3C,SAAS,MAAM,cAAc;AAC7B,SAAS,eAAe;AACxB,SAAS,kBAAkB;AAE3B,SAAS,qBAAqB,MAAM;AAChC,KAAG,qCAAqC,iBAAkB;AACtD,QAAI,CAAC,OAAO,MAAM,EAAE,oCAAoC;AACpD,WAAK,KAAK;AAEd,SAAK,QAAQ,GAAK;AAElB,UAAM,aAAa;AACnB,QAAI,SAAS;AAEb,UAAM,KAAK,MAAM;AAAA,MACb;AAAA;AAAA;AAAA,IAGJ;AAEA,mBAAe,OACX,SAA8B,QACjB;AACb,eAAS,UAAU,OAAO,SAAS,CAAC;AACpC,aAAO,SAAS,QAAQ,IAAI,QAAQ,EAAE;AACtC,YAAM,UAAU;AAChB,YAAM,UAAU;AAAA,IACpB;AAGA,aAAS,IAAI,GAAG,IAAI,GAAG,KAAK;AACxB,YAAM,OAAO;AAAA,IACjB;AACA,UAAM,OAAO,KAAK;AAClB,UAAM,WAAW,MAAM,WAAW;AAElC,aAAS,IAAI,GAAG,IAAI,YAAY,KAAK;AACjC,YAAM,OAAO;AAAA,IACjB;AACA,UAAM,OAAO,KAAK;AAClB,UAAM,UAAU,MAAM,WAAW;AAOjC;AAAA,MACI,QAAQ,QAAQ,SAAS;AAAA,MACzB,mBAAmB,SAAS,KAAK,YAAY,QAAQ,KAAK;AAAA,gBACtD,SAAS,GAAG,YAAY,QAAQ,GAAG;AAAA,eACpC,SAAS,EAAE,YAAY,QAAQ,EAAE;AAAA,mBAC7B,SAAS,MAAM,YAAY,QAAQ,MAAM;AAAA,IACpD,EAAE,GAAG,GAAG,IAAI,CAAC;AAAA,EACjB,CAAC;AACL,CAAC;",
4
+ "sourcesContent": ["/*\nCopyright 2023 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 { fixture } from '@open-wc/testing';\nimport { Default } from '../stories/grid.stories.js';\nimport { Grid } from '@spectrum-web-components/grid';\nimport { testForMemoryLeaks } from '../../../test/testing-helpers.js';\n\ntestForMemoryLeaks(async () => await fixture<Grid>(Default()));\n"],
5
+ "mappings": ";AAYA,SAAS,eAAe;AACxB,SAAS,eAAe;AAExB,SAAS,0BAA0B;AAEnC,mBAAmB,YAAY,MAAM,QAAc,QAAQ,CAAC,CAAC;",
6
6
  "names": []
7
7
  }