From 281071fc551b189afa0ef9ef21e542c27661d567 Mon Sep 17 00:00:00 2001 From: Hunter Loftis Date: Tue, 15 Mar 2022 11:17:42 -0400 Subject: [PATCH] fix: avoid registering lit-virtualizer globally --- tools/grid/src/Grid.ts | 3 +-- tools/grid/src/GridController.ts | 9 ++++----- tools/grid/src/LitVirtualizer.ts | 27 +++++++++++++++++++++++++++ tools/grid/test/grid.test.ts | 20 ++++++++++++++++++++ 4 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 tools/grid/src/LitVirtualizer.ts diff --git a/tools/grid/src/Grid.ts b/tools/grid/src/Grid.ts index 92cfb7a86e..88c1e57b74 100644 --- a/tools/grid/src/Grid.ts +++ b/tools/grid/src/Grid.ts @@ -16,9 +16,8 @@ import { TemplateResult, } from '@spectrum-web-components/base'; import { property } from '@spectrum-web-components/base/src/decorators.js'; -import { LitVirtualizer } from '@lit-labs/virtualizer'; +import { LitVirtualizer } from './LitVirtualizer.js'; import { grid } from '@lit-labs/virtualizer/layouts/grid.js'; - import styles from './grid.css.js'; import { GridController } from './GridController.js'; diff --git a/tools/grid/src/GridController.ts b/tools/grid/src/GridController.ts index f2c36b5f69..bf8837acb6 100644 --- a/tools/grid/src/GridController.ts +++ b/tools/grid/src/GridController.ts @@ -9,7 +9,7 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -import type { ReactiveController } from 'lit'; +import type { ReactiveController, ReactiveElement } from 'lit'; import { ResizeController } from '@lit-labs/observers/resize_controller.js'; import { RovingTabindexController } from '@spectrum-web-components/reactive-controllers/src/RovingTabindex.js'; @@ -17,7 +17,6 @@ import { RangeChangedEvent, VisibilityChangedEvent, } from '@lit-labs/virtualizer/Virtualizer.js'; -import type { LitVirtualizer } from '@lit-labs/virtualizer'; interface ItemSize { width: number; @@ -27,7 +26,7 @@ interface ItemSize { export class GridController implements ReactiveController { - host!: LitVirtualizer; + host!: ReactiveElement; resizeController!: ResizeController; @@ -61,7 +60,7 @@ export class GridController _last = 0; constructor( - host: LitVirtualizer, + host: ReactiveElement, { elements, itemSize, @@ -151,7 +150,7 @@ export class GridController }); }); }; - const scrollToFirst = (): void => this.host.scrollToIndex(0); + const scrollToFirst = (): void => (this.host as any).scrollToIndex(0); const focusIntoGrid = (): void => { this.focus(); this.host.tabIndex = -1; diff --git a/tools/grid/src/LitVirtualizer.ts b/tools/grid/src/LitVirtualizer.ts new file mode 100644 index 0000000000..63853453ab --- /dev/null +++ b/tools/grid/src/LitVirtualizer.ts @@ -0,0 +1,27 @@ +/* +Copyright 2020 Adobe. All rights reserved. +This file is licensed to you under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. You may obtain a copy +of the License at http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS +OF ANY KIND, either express or implied. See the License for the specific language +governing permissions and limitations under the License. +*/ + +// @lit-labs/virtualizer combines defining the LitVirtualizer class with defining `lit-virtualizer` in the global registry. +// Grid extends LitVirtualizer and doesn't need the tag, which can conflict with other uses of lit-virtualizer in the same document. +// This is a hack to keep lit-virtualizer from registering globally. + +const oldDefine = customElements.define; +customElements.define = (name, constructor) => { + if (name === 'lit-virtualizer') return; + oldDefine.call(customElements, name, constructor); +}; + +const { LitVirtualizer } = await import('@lit-labs/virtualizer'); + +customElements.define = oldDefine; + +export { LitVirtualizer }; diff --git a/tools/grid/test/grid.test.ts b/tools/grid/test/grid.test.ts index 5759a6acfd..c8f6cf40c0 100644 --- a/tools/grid/test/grid.test.ts +++ b/tools/grid/test/grid.test.ts @@ -251,4 +251,24 @@ describe('Grid', () => { expect(el.selected).to.deep.equal([{ id: 4 }]); }); + it('does not claim lit-virtualizer on the global registry', async () => { + const test = await fixture( + html` +
${Default()}
+ ` + ); + const el = test.querySelector('sp-grid') as Grid; + + await elementUpdated(el); + + customElements.define('lit-virtualizer', class extends HTMLElement {}); + + // make sure we also don't prevent *any* registration of lit-virtualizer + expect(() => { + customElements.define( + 'lit-virtualizer', + class extends HTMLElement {} + ); + }).to.throw(); + }); });