Skip to content

Commit

Permalink
fix: avoid registering lit-virtualizer globally
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterloftis committed Mar 16, 2022
1 parent c898ca2 commit 281071f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
3 changes: 1 addition & 2 deletions tools/grid/src/Grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
9 changes: 4 additions & 5 deletions tools/grid/src/GridController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ 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';
import {
RangeChangedEvent,
VisibilityChangedEvent,
} from '@lit-labs/virtualizer/Virtualizer.js';
import type { LitVirtualizer } from '@lit-labs/virtualizer';

interface ItemSize {
width: number;
Expand All @@ -27,7 +26,7 @@ interface ItemSize {
export class GridController<T extends HTMLElement>
implements ReactiveController
{
host!: LitVirtualizer;
host!: ReactiveElement;

resizeController!: ResizeController;

Expand Down Expand Up @@ -61,7 +60,7 @@ export class GridController<T extends HTMLElement>
_last = 0;

constructor(
host: LitVirtualizer,
host: ReactiveElement,
{
elements,
itemSize,
Expand Down Expand Up @@ -151,7 +150,7 @@ export class GridController<T extends HTMLElement>
});
});
};
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;
Expand Down
27 changes: 27 additions & 0 deletions tools/grid/src/LitVirtualizer.ts
Original file line number Diff line number Diff line change
@@ -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 };
20 changes: 20 additions & 0 deletions tools/grid/test/grid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLDivElement>(
html`
<div>${Default()}</div>
`
);
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();
});
});

0 comments on commit 281071f

Please sign in to comment.