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

fix(icon): workaround for webkit double render (ssr) #2055

Merged
merged 8 commits into from
Jan 8, 2025
Merged
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
4 changes: 4 additions & 0 deletions .changeset/fifty-news-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
"@rhds/elements": patch
---
`<rh-cta>`: workaround for Safari which sometimes double-renders icons
4 changes: 4 additions & 0 deletions .changeset/rude-jeans-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
"@rhds/elements": patch
---
`<rh-icon>`: workaround for Safari which sometimes double-renders icons
37 changes: 16 additions & 21 deletions elements/rh-cta/rh-cta.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LitElement, html, isServer, noChange } from 'lit';
import { LitElement, html, isServer } from 'lit';
import { customElement } from 'lit/decorators/custom-element.js';
import { property } from 'lit/decorators/property.js';
import { state } from 'lit/decorators/state.js';
import { classMap } from 'lit/directives/class-map.js';
import { ifDefined } from 'lit/directives/if-defined.js';

Expand All @@ -13,7 +14,6 @@ import { colorContextConsumer, type ColorTheme } from '../../lib/context/color/c
import type { IconNameFor, IconSetName } from '@rhds/icons';

import style from './rh-cta.css';
import { state } from 'lit/decorators/state.js';

function isSupportedContent(el: Element | null): el is HTMLAnchorElement | HTMLButtonElement {
return el instanceof HTMLAnchorElement || el instanceof HTMLButtonElement;
Expand Down Expand Up @@ -106,19 +106,13 @@ export class RhCta extends LitElement {
/** when `href` is set, the link's `target` attribute */
@property() target?: string;

/**
* Icon name
*/
/** Icon name */
@property({ reflect: true }) icon?: IconNameFor<IconSetName>;

/**
* Icon set
*/
/** Icon set */
@property({ attribute: 'icon-set' }) iconSet: IconSetName = 'ui';

/**
* Sets color theme based on parent context
*/
/** Sets color theme based on parent context */
@colorContextConsumer() @state() private on?: ColorTheme;

protected override async getUpdateComplete(): Promise<boolean> {
Expand All @@ -142,17 +136,12 @@ export class RhCta extends LitElement {
const rtl = this.#dir.dir === 'rtl';
const isDefault = !variant;
const svg = isDefault;
const iconOrSvg = isDefault || !!icon;
const follower =
(variant !== 'brick' && icon) ?
html`<rh-icon .icon=${icon} .set=${iconSet ?? 'ui'}></rh-icon>`
: (variant === undefined) ?
html`<rh-icon set="ui" icon="arrow-right"></rh-icon>`
: html``;
(variant !== 'brick' && icon) ? html`<rh-icon icon=${icon} set=${iconSet ?? 'ui'}></rh-icon>`
: (variant === undefined) ? html`<rh-icon icon="arrow-right" set="ui"></rh-icon>`
: '';
const iconContent =
(variant === 'brick' && icon) ?
html`<rh-icon .icon=${icon} set="${iconSet ?? 'ui'}"></rh-icon>`
: html``;
!(variant === 'brick' && icon) ? '' : html`<rh-icon .icon=${icon} set="${iconSet ?? 'ui'}"></rh-icon>`;
const linkContent =
!href ? html`<slot></slot>${follower}`
: html`<a href=${href}
Expand All @@ -168,10 +157,16 @@ export class RhCta extends LitElement {
}

override firstUpdated() {
const { href, variant } = this;
// workaround for lit-ssr bugs
if (!isServer) {
this.removeAttribute('defer-hydration');
const [, ...duplicateContainers] = this.shadowRoot?.querySelectorAll('#container') ?? [];
for (const dupe of duplicateContainers) {
dupe.remove();
}
}
// TODO: remove in next major version, recommend static HTML audits instead
const { href, variant } = this;
const cta =
this.shadowRoot?.querySelector('a')
?? this.shadowRoot?.querySelector('slot')?.assignedElements().find(isSupportedContent)
Expand Down
8 changes: 8 additions & 0 deletions elements/rh-icon/rh-icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ export class RhIcon extends LitElement {
`;
}

updated() {
// this is a workaround for an apparent webkit / lit-ssr bug
const [, ...duplicateContainers] = this.shadowRoot?.querySelectorAll('#container') ?? [];
for (const dupe of duplicateContainers) {
dupe.remove();
}
}

#getContent() {
if (isServer) {
const { set = 'standard', icon } = this;
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@
"@commitlint/cli": "^19.5.0",
"@commitlint/config-conventional": "^19.5.0",
"@lit-labs/eleventy-plugin-lit": "^1.0.3",
"@lit-labs/ssr": "^3.2.2",
"@lit-labs/ssr-client": "^1.1.7",
"@lit/reactive-element": "^2.0.4",
"@lit/ts-transformers": "^2.0.1",
"@parse5/tools": "^0.5.0",
Expand Down
Loading