Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: lightdom css controller #2057

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions docs/_includes/layouts/pages/element.11ty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ export default class ElementsPage extends Renderer<Context> {
} = doc;
const content = fileExists ? await this.renderFile(filePath, ctx) : '';
const stylesheets = [
'/assets/packages/@rhds/elements/elements/rh-table/rh-table-lightdom.css',
// '/assets/packages/@rhds/elements/elements/rh-table/rh-table-lightdom.css',
'/styles/samp.css',
ctx.doc.hasLightdom && `/assets/packages/@rhds/elements/elements/${tagName}/${tagName}-lightdom.css`,
// ctx.doc.hasLightdom && `/assets/packages/@rhds/elements/elements/${tagName}/${tagName}-lightdom.css`,
isCodePage && '/styles/pages/code.css',
].filter(Boolean);

Expand Down Expand Up @@ -961,4 +961,3 @@ export default class ElementsPage extends Renderer<Context> {
}
}
};

9 changes: 8 additions & 1 deletion docs/_plugins/lit-ssr/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ class RHDSSSRableRenderer extends LitElementRenderer {
}
yield* super.renderShadow(renderInfo);
}

override* renderLight(renderInfo: RenderInfo): RenderResult {
if (this.tagName === 'rh-table') {
console.log(renderInfo.customElementHostStack);
console.log(this.element);
}
yield* super.renderLight(renderInfo);
}
}

const elementRenderers = [
Expand Down Expand Up @@ -93,4 +101,3 @@ export default async function renderPage({
const end = performance.now();
return { page, rendered, durationMs: end - start };
}

2 changes: 0 additions & 2 deletions elements/rh-table/demo/rh-table.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
<small slot="summary">Dates and venues subject to change.</small>
</rh-table>

<link rel="stylesheet" href="../rh-table-lightdom.css">

<script type="module">
import '@rhds/elements/rh-table/rh-table.js';
</script>
6 changes: 3 additions & 3 deletions elements/rh-table/rh-sort-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import { customElement } from 'lit/decorators/custom-element.js';
import { property } from 'lit/decorators/property.js';
import { classMap } from 'lit/directives/class-map.js';

import { ComposedEvent } from '@patternfly/pfe-core';

import { colorContextConsumer, type ColorTheme } from '../../lib/context/color/consumer.js';

import styles from './rh-sort-button.css';

const DIRECTIONS_OPPOSITES = { asc: 'desc', desc: 'asc' } as const;

export class RequestSortEvent extends ComposedEvent {
export class RequestSortEvent extends Event {
constructor(public direction: 'asc' | 'desc') {
super('request-sort', {
bubbles: true,
composed: true,
cancelable: true,
});
}
Expand Down
7 changes: 7 additions & 0 deletions elements/rh-table/rh-table-lightdom.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
@property --_rhds-lightdom {
inherits: false;
syntax: string;
}

rh-table {
--_rhds-lightdom: rh-table;

container: host / inline-size;

& thead {
Expand Down
8 changes: 7 additions & 1 deletion elements/rh-table/rh-table.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { ColorTheme } from '@rhds/elements/lib/context/color/consumer.js';

import { LitElement, html, isServer } from 'lit';
import { customElement } from 'lit/decorators/custom-element.js';
import { classMap } from 'lit/directives/class-map.js';
Expand All @@ -6,7 +8,8 @@ import { Logger } from '@patternfly/pfe-core/controllers/logger.js';

import { RequestSortEvent, RhSortButton } from './rh-sort-button.js';

import { colorContextConsumer, type ColorTheme } from '../../lib/context/color/consumer.js';
import { colorContextConsumer } from '@rhds/elements/lib/context/color/consumer.js';
import { LightdomCSSController } from '@rhds/elements/lib/LightdomCSSController.js';

import styles from './rh-table.css';

Expand Down Expand Up @@ -68,6 +71,9 @@ export class RhTable extends LitElement {
return this.querySelector('[slot="summary"]') as HTMLElement | undefined;
}

// eslint-disable-next-line no-unused-private-class-members
#lightdom = new LightdomCSSController(this, import.meta.url);

#internalColorPalette?: string | null;

#logger = new Logger(this);
Expand Down
50 changes: 50 additions & 0 deletions lib/LightdomCSSController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { ReactiveController, ReactiveElement } from 'lit';

import { Logger } from '@patternfly/pfe-core/controllers/logger.js';
import type { RHDSSSRController } from './ssr-controller.js';

const handleAsText = (r: Response) => r.text();

export class LightdomCSSController implements ReactiveController, RHDSSSRController {
isRHDSSSRController = true;

host: ReactiveElement;

#tagName: string;

/** URL to lightdom sheet, by convention it must be the tagName, with `-lightdom.css` suffix */
#url: URL;

#logger: Logger;

#computedStyles?: CSSStyleDeclaration;

constructor(
host: ReactiveElement,
/** import.meta.url from the element definition module */
url: string,
) {
this.host = host;
this.#tagName = host.localName;
this.#url = new URL(`${this.#tagName}-lightdom.css`, url);
this.#logger = new Logger(host);
host.addController(this);
}

async hostConnected(): Promise<void> {
this.#computedStyles ??= getComputedStyle(this.host);
if (this.#computedStyles.getPropertyValue('--_rhds-lightdom') !== this.#tagName) {
const root = this.host.getRootNode();
if (root instanceof Document || root instanceof ShadowRoot) {
try {
const sheet = new CSSStyleSheet();
const css = await fetch(this.#url).then(handleAsText);
sheet.replaceSync(css);
root.adoptedStyleSheets = [...root.adoptedStyleSheets, sheet];
} catch (error) {
this.#logger.error(error);
}
}
}
};
}
2 changes: 0 additions & 2 deletions uxdot/uxdot-repo-status-checklist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ export class UxdotRepoStatusChecklist extends UxdotRepoElement {
render() {
const status = this.getStatus(this.element!);
return html`
<!-- TODO: remove lightdom after implementing auto-load-->
<link rel="stylesheet" href="/assets/packages/@rhds/elements/elements/rh-table/rh-table-lightdom.css">
<div id="container">
<rh-table>
<table>
Expand Down
2 changes: 0 additions & 2 deletions uxdot/uxdot-repo-status-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export class UxdotRepoStatusList extends UxdotRepoElement {
render() {
const status = this.getStatus(this.element!);
return html`
<!-- TODO: remove lightdom after implementing auto-load-->
<link rel="stylesheet" href="/assets/packages/@rhds/elements/elements/rh-table/rh-table-lightdom.css">
<div id="container">
<a href="#status-checklist" class="checklist">What do these mean?</a>
<div id="inner-container">
Expand Down
2 changes: 0 additions & 2 deletions uxdot/uxdot-repo-status-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ export class UxdotRepoStatusTable extends UxdotRepoElement {
render() {
const status = this.getStatus();
return html`
<!-- TODO: remove lightdom after implementing auto-load-->
<link rel="stylesheet" href="/assets/packages/@rhds/elements/elements/rh-table/rh-table-lightdom.css">
<div id="container">
<rh-table>
<table>
Expand Down
2 changes: 0 additions & 2 deletions uxdot/uxdot-spacer-tokens-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ export class UxdotSpacerTokensTable extends LitElement {
.filter(Boolean);

return html`
<!-- TODO: remove lightdom after implementing auto-load-->
<link rel="stylesheet" href="/assets/packages/@rhds/elements/elements/rh-table/rh-table-lightdom.css">
<rh-table color-palette="${this.colorPalette}">
<table>
<caption>${this.caption}</caption>
Expand Down
Loading