mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-30 12:46:35 +00:00
stateSet polyfill
This commit is contained in:
parent
de0d91e14f
commit
85f0ad161a
@ -2,6 +2,8 @@ import Button from "@awesome.me/webawesome/dist/components/button/button";
|
|||||||
import { css, type CSSResultGroup } from "lit";
|
import { css, type CSSResultGroup } from "lit";
|
||||||
import { customElement } from "lit/decorators";
|
import { customElement } from "lit/decorators";
|
||||||
|
|
||||||
|
import { StateSet } from "../resources/polyfills/stateset";
|
||||||
|
|
||||||
export type Appearance = "accent" | "filled" | "outlined" | "plain";
|
export type Appearance = "accent" | "filled" | "outlined" | "plain";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -36,6 +38,14 @@ export type Appearance = "accent" | "filled" | "outlined" | "plain";
|
|||||||
export class HaButton extends Button {
|
export class HaButton extends Button {
|
||||||
variant: "brand" | "neutral" | "success" | "warning" | "danger" = "brand";
|
variant: "brand" | "neutral" | "success" | "warning" | "danger" = "brand";
|
||||||
|
|
||||||
|
attachInternals() {
|
||||||
|
const internals = super.attachInternals();
|
||||||
|
Object.defineProperty(internals, "states", {
|
||||||
|
value: new StateSet(this, internals.states),
|
||||||
|
});
|
||||||
|
return internals;
|
||||||
|
}
|
||||||
|
|
||||||
static get styles(): CSSResultGroup {
|
static get styles(): CSSResultGroup {
|
||||||
return [
|
return [
|
||||||
Button.styles,
|
Button.styles,
|
||||||
|
57
src/resources/polyfills/stateset.ts
Normal file
57
src/resources/polyfills/stateset.ts
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
// A small polyfill for CSSStateSet
|
||||||
|
export class StateSet extends Set<string> {
|
||||||
|
private _el: Element;
|
||||||
|
|
||||||
|
private _existing: null | Set<string> = null;
|
||||||
|
|
||||||
|
constructor(el: Element, existing: Set<string> | null = null) {
|
||||||
|
super();
|
||||||
|
this._el = el;
|
||||||
|
this._existing = existing;
|
||||||
|
}
|
||||||
|
|
||||||
|
add(state: string) {
|
||||||
|
super.add(state);
|
||||||
|
const existing = this._existing;
|
||||||
|
if (existing) {
|
||||||
|
try {
|
||||||
|
existing.add(state);
|
||||||
|
} catch {
|
||||||
|
existing.add(`--${state}`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this._el.setAttribute(`state-${state}`, "");
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
delete(state: string) {
|
||||||
|
super.delete(state);
|
||||||
|
const existing = this._existing;
|
||||||
|
if (existing) {
|
||||||
|
existing.delete(state);
|
||||||
|
existing.delete(`--${state}`);
|
||||||
|
} else {
|
||||||
|
this._el.removeAttribute(`state-${state}`);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
has(state: string) {
|
||||||
|
return super.has(state);
|
||||||
|
}
|
||||||
|
|
||||||
|
clear() {
|
||||||
|
for (const state of this) this.delete(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const replaceSync = CSSStyleSheet.prototype.replaceSync;
|
||||||
|
Object.defineProperty(CSSStyleSheet.prototype, "replaceSync", {
|
||||||
|
value: function (text) {
|
||||||
|
text = text.replace(
|
||||||
|
/:state\(([^)]+)\)/g,
|
||||||
|
":where(:state($1), :--$1, [state-$1])"
|
||||||
|
);
|
||||||
|
replaceSync.call(this, text);
|
||||||
|
},
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user