Compare commits

..

23 Commits

Author SHA1 Message Date
Lea Verou
f753dc8e76 Merge branch 'icon-properties' of https://github.com/shoelace-style/webawesome into icon-properties 2025-01-07 17:55:42 -05:00
Lea Verou
4bee37f621 Remove docs from alpha 2025-01-07 17:55:32 -05:00
Lea Verou
4103cea723 Better docs 2025-01-07 17:55:32 -05:00
Lea Verou
f12de60739 Rename to cssAttributeProperties, follow property name if specified 2025-01-07 17:55:32 -05:00
Lea Verou
b32f4d2d13 Move css property reflection stuff to base class 2025-01-07 17:55:32 -05:00
Lea Verou
e94c8b34b8 CSS-set properties should not override JS-set properties 2025-01-07 17:55:32 -05:00
Lea Verou
5ca8517acf Update icon.md 2025-01-07 17:55:31 -05:00
Lea Verou
14ca7e2bd8 Update icon.md 2025-01-07 17:55:31 -05:00
Lea Verou
01834120c9 Fix deploy 2025-01-07 17:55:31 -05:00
Lea Verou
fe1cf2d72d Start work on --wa-icon-* properties 2025-01-07 17:55:31 -05:00
Lea Verou
a66a926ce4 Remove docs from alpha 2025-01-07 17:51:49 -05:00
Lea Verou
423067c7a7 Better docs 2025-01-07 17:44:22 -05:00
Lea Verou
f31d19c249 Rename to cssAttributeProperties, follow property name if specified 2025-01-07 17:41:22 -05:00
Lea Verou
711cf99cf8 Move css property reflection stuff to base class 2025-01-07 16:55:50 -05:00
Lea Verou
e4117512f8 Harmonize updated() definitions
- Use proper type
- Use same argument name
- Ensure `super.updated()` is called
2025-01-07 16:53:08 -05:00
Lea Verou
0784f39537 CSS-set properties should not override JS-set properties 2025-01-07 15:05:18 -05:00
Lea Verou
9d2bd6108d Update icon.md 2025-01-07 14:44:51 -05:00
Lea Verou
7b91d41768 Update icon.md 2025-01-07 14:37:15 -05:00
Lea Verou
a6847886e8 Merge branch 'next' into icon-properties 2025-01-07 14:23:56 -05:00
Lea Verou
5fdfc2f1fa Merge branch 'next' into icon-properties 2025-01-07 12:10:34 -05:00
Lea Verou
cd5c8046a0 Fix deploy 2025-01-07 10:23:53 -05:00
Lea Verou
2db378cfae Merge branch 'next' into icon-properties 2025-01-07 10:17:39 -05:00
Lea Verou
7c4dc7e051 Start work on --wa-icon-* properties 2025-01-06 22:02:48 -05:00
5 changed files with 6 additions and 73 deletions

View File

@@ -28,8 +28,7 @@ Many Font Awesome Pro icon families have variants such as `thin`, `light`, `regu
### Setting icon info via CSS
You can also set the icon's family, name, and variant via CSS custom properties.
This can be useful when you want to set the icon dynamically (e.g. in response to a CSS pseudo-class or media query) or set defaults for a group of icons (e.g. icons inside callouts or all icons for a given theme).
You can set the icon's family, name, and variant via CSS custom properties. This can be useful when you want to set <!-- the icon dynamically or set --> defaults for a group of icons (e.g. icons inside callouts or all icons for a given theme):
```html {.example}
<wa-callout>
@@ -47,7 +46,6 @@ This can be useful when you want to set the icon dynamically (e.g. in response t
<!-- Look ma, no attributes! -->
<wa-icon slot="icon"></wa-icon>
Here be dragons.
<button id="toggle_icon">Toggle&nbsp;<wa-icon name="circle-exclamation"></wa-icon></button>
</wa-callout>
<style>
@@ -60,18 +58,6 @@ wa-callout {
}
}
</style>
<script>
toggle_icon.addEventListener('click', e => {
let callout = e.target.closest('wa-callout');
let value = callout.style.getPropertyValue('--wa-icon-name').trim();
if (value) {
callout.style.removeProperty('--wa-icon-name');
}
else {
callout.style.setProperty('--wa-icon-name', 'circle-exclamation');
}
});
</script>
```
Notes:

10
package-lock.json generated
View File

@@ -9,7 +9,6 @@
"version": "3.0.0-alpha.7",
"license": "MIT",
"dependencies": {
"@bramus/style-observer": "^1.3.0",
"@ctrl/tinycolor": "^4.1.0",
"@floating-ui/dom": "^1.6.12",
"@shoelace-style/animations": "^1.2.0",
@@ -677,15 +676,6 @@
"node": ">=4"
}
},
"node_modules/@bramus/style-observer": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/@bramus/style-observer/-/style-observer-1.3.0.tgz",
"integrity": "sha512-IQjYId9X7xgz0NeKGatC37lqsdS+cE5bfdB9jKh7+zJnA9BqENee2C48boDIRQrTED4JxleRZGhTY86S1/l7QA==",
"license": "MIT",
"engines": {
"node": ">=10"
}
},
"node_modules/@cspell/cspell-bundled-dicts": {
"version": "6.31.3",
"resolved": "https://registry.npmjs.org/@cspell/cspell-bundled-dicts/-/cspell-bundled-dicts-6.31.3.tgz",

View File

@@ -64,7 +64,6 @@
"node": ">=14.17.0"
},
"dependencies": {
"@bramus/style-observer": "^1.3.0",
"@ctrl/tinycolor": "^4.1.0",
"@floating-ui/dom": "^1.6.12",
"@shoelace-style/animations": "^1.2.0",

View File

@@ -31,24 +31,3 @@ svg {
width: 1em;
justify-content: center;
}
@property --wa-icon-family {
syntax: '<custom-ident> | auto';
inherits: true;
initial-value: 'auto';
}
@property --wa-icon-variant {
syntax: '<custom-ident> | auto';
inherits: true;
initial-value: 'auto';
}
@property --wa-icon-library {
syntax: '<custom-ident> | auto';
inherits: true;
initial-value: 'auto';
}
@property --wa-icon-name {
syntax: '<custom-ident> | auto';
inherits: true;
initial-value: 'auto';
}

View File

@@ -1,4 +1,3 @@
import { CSSStyleObserver } from '@bramus/style-observer';
import type { CSSResult, CSSResultGroup, PropertyDeclaration, PropertyValues } from 'lit';
import { LitElement, isServer, unsafeCSS } from 'lit';
import { property } from 'lit/decorators.js';
@@ -71,8 +70,6 @@ export default class WebAwesomeElement extends LitElement {
connectedCallback() {
super.connectedCallback();
// FIXME this is currently static.
// It will only update when the element is connected, not when a relevant CSS property changes.
this.updateCSSProperties();
}
@@ -194,36 +191,19 @@ export default class WebAwesomeElement extends LitElement {
return;
}
if (!Self.styleObserver) {
// First time, init stuff
// First, replace `true` with actual CSS property names
for (let [name, cssProperty] of Self.cssAttributeProperties) {
if (cssProperty === true) {
// Default name
cssProperty = `--${this.tagName.toLowerCase()}-${name}`;
Self.cssAttributeProperties.set(name, cssProperty);
}
}
// Then we observe them
let cssProperties = [...Self.cssAttributeProperties.values()] as string[];
Self.styleObserver = new CSSStyleObserver(cssProperties, (...args) => {
console.log(...args);
this.updateCSSProperties();
});
}
this.#computedStyle ??= getComputedStyle(this);
// FIXME this is currently static. It will only update when the element is connected, and not when the CSS property changes.
const tagName = this.tagName.toLowerCase();
for (let [name, cssProperty] of Self.cssAttributeProperties) {
// FIXME currently this means that CSS properties will override JS properties. This is not ideal.
if (typeof name === 'string' && !this.hasAttribute(name) && this.#setVia[name] !== 'js') {
// Check if supplied as a CSS custom property
// TODO !important should override attribute values
cssProperty = cssProperty === true ? `--${tagName}-${name}` : cssProperty;
const value = this.#computedStyle?.getPropertyValue(cssProperty);
if (value && value !== 'auto') {
if (value) {
this.#setVia[name] = 'css';
this.#setting.add(name);
// @ts-ignore
@@ -236,16 +216,15 @@ export default class WebAwesomeElement extends LitElement {
}
}
// Subclasses will get their own copy automagically (see below)
// Subclasses will override this
protected static cssAttributeProperties = new Map<PropertyKey, true | string>();
protected static styleObserver: CSSStyleObserver | undefined;
static createProperty(name: PropertyKey, options?: PropertyDeclaration): void {
super.createProperty(name, options);
if (options?.cssProperty) {
if (this.cssAttributeProperties === WebAwesomeElement.cssAttributeProperties) {
// Each class needs its own, otherwise they'd share the same object
// Each class needs its own, otherwise they'd share the same Set
this.cssAttributeProperties = new Map();
}