Skip to content

Commit

Permalink
fix(query-template-by-id.ts): fix setter to uncache oldValue, enroll …
Browse files Browse the repository at this point in the history
…in requestUpdate, improve types
  • Loading branch information
waldronmatt committed Sep 1, 2024
1 parent 5350ca1 commit c4e3974
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 85 deletions.
25 changes: 22 additions & 3 deletions packages/lit-override/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,13 @@ Add the following to the component you want to override styles and markup on:

```ts
import { LitElement, html } from 'lit';
import { property } from 'lit/decorators.js';
import { templateContentWithFallback } from '@waldronmatt/lit-override/directives/template-content-with-fallback.js';
import { AdoptedStyleSheetsConverter } from '@waldronmatt/lit-override/controllers/adopted-stylesheets-converter.js';
import { queryTemplateById } from '@waldronmatt/lit-override/decorators/query-template-by-id.js';
import { queryTemplateById, TemplateIdProperty } from '@waldronmatt/lit-override/decorators/query-template-by-id.js';

export class ChildComponent extends LitElement {
@queryTemplateById({ fallback: true })
templateId!: HTMLTemplateElement | null;
templateId!: TemplateIdProperty['templateIdGetter'];

connectedCallback() {
super.connectedCallback();
Expand Down Expand Up @@ -130,6 +129,26 @@ Set `templateId` on `child-component` to point to the template from which to add
</body>
```

**Note**: You can assign `templateId` an id that references a different template programtically using type assertion:

`child-component.ts`

```ts
import { LitElement } from 'lit';
import { queryTemplateById, TemplateIdProperty } from '@waldronmatt/lit-override/decorators/query-template-by-id.js';

export class ChildComponent extends LitElement {
@queryTemplateById({ fallback: true })
templateId!: TemplateIdProperty['templateIdGetter'];

connectedCallback() {
super.connectedCallback();

(this.templateId as TemplateIdProperty['templateIdSetter']) = 'otherChildTemplate';
}
}
```

### Lit Override Component

The `<lit-override>` component provides an all-in-one approach to override markup and styles in the light DOM and shadow DOM:
Expand Down
98 changes: 49 additions & 49 deletions packages/lit-override/custom-elements.json
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,55 @@
}
]
},
{
"kind": "javascript-module",
"path": "src/decorators/index.ts",
"declarations": [],
"exports": [
{
"kind": "js",
"name": "*",
"declaration": {
"name": "*",
"package": "./query-template-by-id.js"
}
}
]
},
{
"kind": "javascript-module",
"path": "src/decorators/query-template-by-id.ts",
"declarations": [
{
"kind": "function",
"name": "queryTemplateById",
"parameters": [
{
"name": "{ fallback = false }",
"default": "{}",
"type": {
"text": "QueryTemplateByIdParams"
}
},
{
"description": "gets a template element if an id is not provided (not cached). Defaults to `false`.",
"name": "fallback"
}
],
"description": "queryTemplateById\n\nGets a template element by id that is provided to the `templateId` property.\nWill cache the template element on successful query."
}
],
"exports": [
{
"kind": "js",
"name": "queryTemplateById",
"declaration": {
"name": "queryTemplateById",
"module": "src/decorators/query-template-by-id.ts"
}
}
]
},
{
"kind": "javascript-module",
"path": "src/controllers/adopted-stylesheets-converter.ts",
Expand Down Expand Up @@ -391,55 +440,6 @@
}
]
},
{
"kind": "javascript-module",
"path": "src/decorators/index.ts",
"declarations": [],
"exports": [
{
"kind": "js",
"name": "*",
"declaration": {
"name": "*",
"package": "./query-template-by-id.js"
}
}
]
},
{
"kind": "javascript-module",
"path": "src/decorators/query-template-by-id.ts",
"declarations": [
{
"kind": "function",
"name": "queryTemplateById",
"parameters": [
{
"name": "{ fallback = false }",
"default": "{}",
"type": {
"text": "QueryTemplateByIdParams"
}
},
{
"description": "gets a template element if an id is not provided (not cached). Defaults to `false`.",
"name": "fallback"
}
],
"description": "queryTemplateById\n\nGets a template element by id that is provided to the `templateId` property.\nWill cache the template element on successful query."
}
],
"exports": [
{
"kind": "js",
"name": "queryTemplateById",
"declaration": {
"name": "queryTemplateById",
"module": "src/decorators/query-template-by-id.ts"
}
}
]
},
{
"kind": "javascript-module",
"path": "src/mixins/emit-connected-callback.ts",
Expand Down
48 changes: 24 additions & 24 deletions packages/lit-override/custom-elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,30 @@
| `js` | `default` | LitOverride | src/components/lit-override.ts | |
| `custom-element-definition` | `lit-override` | LitOverride | /src/components/lit-override-component.js | |

## `src/decorators/index.ts`:

### Exports

| Kind | Name | Declaration | Module | Package |
| ---- | ---- | ----------- | ------ | ------------------------- |
| `js` | `*` | \* | | ./query-template-by-id.js |

## `src/decorators/query-template-by-id.ts`:

### Functions

| Name | Description | Parameters | Return |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------- | ------ |
| `queryTemplateById` | queryTemplateById&#xA;&#xA;Gets a template element by id that is provided to the \`templateId\` property.&#xA;Will cache the template element on successful query. | `{ fallback = false }: QueryTemplateByIdParams, fallback` | |

<hr/>

### Exports

| Kind | Name | Declaration | Module | Package |
| ---- | ------------------- | ----------------- | -------------------------------------- | ------- |
| `js` | `queryTemplateById` | queryTemplateById | src/decorators/query-template-by-id.ts | |

## `src/controllers/adopted-stylesheets-converter.ts`:

### class: `AdoptedStyleSheetsConverter`
Expand Down Expand Up @@ -134,30 +158,6 @@
| `js` | `templateContentWithFallback` | templateContentWithFallback | src/directives/template-content-with-fallback.ts | |
| `js` | `TemplateContentWithFallbackDirective` | TemplateContentWithFallbackDirective | src/directives/template-content-with-fallback.ts | |

## `src/decorators/index.ts`:

### Exports

| Kind | Name | Declaration | Module | Package |
| ---- | ---- | ----------- | ------ | ------------------------- |
| `js` | `*` | \* | | ./query-template-by-id.js |

## `src/decorators/query-template-by-id.ts`:

### Functions

| Name | Description | Parameters | Return |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------- | ------ |
| `queryTemplateById` | queryTemplateById&#xA;&#xA;Gets a template element by id that is provided to the \`templateId\` property.&#xA;Will cache the template element on successful query. | `{ fallback = false }: QueryTemplateByIdParams, fallback` | |

<hr/>

### Exports

| Kind | Name | Declaration | Module | Package |
| ---- | ------------------- | ----------------- | -------------------------------------- | ------- |
| `js` | `queryTemplateById` | queryTemplateById | src/decorators/query-template-by-id.ts | |

## `src/mixins/emit-connected-callback.ts`:

### class: `EmitConnectedCallbackProps`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { LitElement, html } from 'lit';
import { EmitConnectedCallback } from '../mixins/emit-connected-callback.js';
import { templateContentWithFallback } from '../directives/template-content-with-fallback.js';
import { AdoptedStyleSheetsConverter } from '../controllers/adopted-stylesheets-converter.js';
import { queryTemplateById } from '../decorators/query-template-by-id.js';
import { queryTemplateById, TemplateIdProperty } from '../decorators/query-template-by-id.js';

/**
* LitOverride - `<lit-override>`
Expand All @@ -17,7 +17,7 @@ import { queryTemplateById } from '../decorators/query-template-by-id.js';
*/
export class LitOverride extends EmitConnectedCallback(LitElement) {
@queryTemplateById({ fallback: true })
templateId!: HTMLTemplateElement | null;
templateId!: TemplateIdProperty['templateIdGetter'];

connectedCallback() {
super.connectedCallback();
Expand Down
21 changes: 14 additions & 7 deletions packages/lit-override/src/decorators/query-template-by-id.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { property } from 'lit/decorators.js';
import { ReactiveElement } from 'lit';

export interface TemplateIdProperty {
get templateIdGetter(): HTMLTemplateElement | null;
set templateIdSetter(value: string | null);
}

interface ExtendedElement extends ReactiveElement {
[key: symbol]: string | null;
_templateCache?: Record<string, HTMLTemplateElement | null>;
[key: symbol]: TemplateIdProperty['templateIdSetter'];
_templateCache?: Record<string, TemplateIdProperty['templateIdGetter']>;
}

export interface QueryTemplateByIdParams {
Expand All @@ -25,7 +30,7 @@ export const queryTemplateById = ({ fallback = false }: QueryTemplateByIdParams
property({ reflect: true, type: String })(proto, propName);

Object.defineProperty(proto, 'templateId', {
get(this: ExtendedElement): HTMLTemplateElement | null {
get(this: ExtendedElement): TemplateIdProperty['templateIdGetter'] {
const id = this[internalKey];

if (!this._templateCache) {
Expand All @@ -38,7 +43,6 @@ export const queryTemplateById = ({ fallback = false }: QueryTemplateByIdParams

if (typeof id === 'string' && id) {
const templateElement = document.querySelector(`template#${id}`) as HTMLTemplateElement | null;

if (!templateElement) {
console.error(`Template id ${id} could not be found`);
return null;
Expand All @@ -50,12 +54,15 @@ export const queryTemplateById = ({ fallback = false }: QueryTemplateByIdParams

return fallback ? document.querySelector('template') : null;
},
set(this: ExtendedElement, value: string | null) {
set(this: ExtendedElement, value: TemplateIdProperty['templateIdSetter']) {
const oldValue = this[internalKey];
this[internalKey] = value;

if (this._templateCache && value) {
delete this._templateCache[value];
if (this._templateCache && oldValue) {
delete this._templateCache[oldValue];
}

this.requestUpdate('templateId', oldValue);
},
enumerable: true,
configurable: true,
Expand Down

0 comments on commit c4e3974

Please sign in to comment.