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
16 changed files with 226 additions and 123 deletions

View File

@@ -19,40 +19,43 @@ icon: callout
Set the `variant` attribute to change the callout's variant.
```html {.example}
{% for variant, info in {
brand: {
title: 'This is super informative',
content: 'You can tell by how pretty the callout is.',
icon: 'circle-info'
},
success: {
title: 'Your changes have been saved',
content: 'You can safely exit the app now.',
icon: 'circle-check'
},
neutral: {
title: 'Your settings have been updated',
content: 'Settings will take effect on next login.',
icon: 'gear'
},
warning: {
title: 'Your session has ended',
content: 'Please login again to continue.',
icon: 'triangle-exclamation'
},
danger: {
title: 'Your account has been deleted',
content: 'Were very sorry to see you go!',
icon: 'circle-exclamation'
}
} %}
<wa-callout variant="{{ variant }}">
<wa-icon slot="icon" name="{{ info.icon }}" variant="regular"></wa-icon>
<strong>{{ info.title }}</strong><br />
{{ info.content }}
</wa-callout>
{% if not loop.last %}<br />{% endif %}
{% endfor %}
<wa-callout variant="brand">
<wa-icon slot="icon" name="circle-info" variant="regular"></wa-icon>
<strong>This is super informative</strong><br />
You can tell by how pretty the callout is.
</wa-callout>
<br />
<wa-callout variant="success">
<wa-icon slot="icon" name="circle-check" variant="regular"></wa-icon>
<strong>Your changes have been saved</strong><br />
You can safely exit the app now.
</wa-callout>
<br />
<wa-callout variant="neutral">
<wa-icon slot="icon" name="gear" variant="regular"></wa-icon>
<strong>Your settings have been updated</strong><br />
Settings will take effect on next login.
</wa-callout>
<br />
<wa-callout variant="warning">
<wa-icon slot="icon" name="triangle-exclamation" variant="regular"></wa-icon>
<strong>Your session has ended</strong><br />
Please login again to continue.
</wa-callout>
<br />
<wa-callout variant="danger">
<wa-icon slot="icon" name="circle-exclamation" variant="regular"></wa-icon>
<strong>Your account has been deleted</strong><br />
We're very sorry to see you go!
</wa-callout>
```
### Appearance

View File

@@ -24,6 +24,49 @@ Many Font Awesome Pro icon families have variants such as `thin`, `light`, `regu
<wa-icon family="brands" name="web-awesome"></wa-icon>
```
<div data-alpha="remove">
### Setting icon info via CSS
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>
<!-- Look ma, no attributes! -->
<wa-icon slot="icon"></wa-icon>
This is a callout.
</wa-callout>
<wa-callout variant="danger">
<wa-icon slot="icon" name="dumpster-fire" variant="solid"></wa-icon>
This is a callout with an explicit icon.
</wa-callout>
<wa-callout variant="warning">
<!-- Look ma, no attributes! -->
<wa-icon slot="icon"></wa-icon>
Here be dragons.
</wa-callout>
<style>
wa-callout {
--wa-icon-variant: regular;
--wa-icon-name: info-circle;
&[variant="warning"] {
--wa-icon-name: triangle-exclamation;
}
}
</style>
```
Notes:
- If you specify attributes, they will override the CSS custom properties, which provides a way to set defaults and then override them as needed.
- CSS custom properties inherit — so if you set a `--wa-icon-*` custom property on an element, it will affect *all* icons within it that dont override these values (either via attributes or CSS custom properties).
- These CSS properties are currently not reactive and will only be read when the component is first connected.
</div>
### Colors
Icons inherit their color from the current text color. Thus, you can set the `color` property on the `<wa-icon>` element or an ancestor to change the color.

View File

@@ -172,7 +172,7 @@ layout: page-outline
<div class="shadow" style="box-shadow: var(--wa-shadow-l);"></div>
```
## Alignment Tests
## Tests
```html {.example}
<style>
@@ -254,8 +254,6 @@ layout: page-outline
</div>
```
## Custom Property Tests
```html {.example}
<style>
.wa-theme-test {
@@ -321,32 +319,4 @@ layout: page-outline
</wa-select>
</div>
```
## Text Controls Tests
```html {.example}
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 2rem;">
<label>Native Input <input type="text" value="value"></label>
<wa-input label="WA Input" type="text" value="value"></wa-input>
<label>
Native Select
<select value="option-1">
<option value="option-1">Option 1</option>
<option value="option-2">Option 2</option>
<option value="option-3">Option 3</option>
<option value="option-4">Option 4</option>
</select>
</label>
<wa-select label="WA Select" value="option-1">
<wa-option value="option-1">Option 1</wa-option>
<wa-option value="option-2">Option 2</wa-option>
<wa-option value="option-3">Option 3</wa-option>
<wa-option value="option-4">Option 4</wa-option>
</wa-select>
<label>Native Textarea <textarea>value</textarea></label>
<wa-textarea label="WA Textarea" value="value"></wa-textarea>
</div>
```

View File

@@ -69,7 +69,7 @@ export default class WaButton extends WebAwesomeFormAssociatedElement {
@property() title = ''; // make reactive to pass through
/** The button's theme variant. */
@property() variant: 'neutral' | 'brand' | 'success' | 'warning' | 'danger' = 'neutral';
@property({ reflect: true }) variant: 'neutral' | 'brand' | 'success' | 'warning' | 'danger' = 'neutral';
/** The button's visual appearance. */
@property({ reflect: true }) appearance: 'accent' | 'filled' | 'outlined' | 'plain' = 'accent';

View File

@@ -317,8 +317,8 @@
border-radius: inherit;
background-color: currentColor;
box-shadow:
inset 0 0 0 var(--border-width) var(--wa-form-control-border-color),
inset 0 0 0 calc(var(--border-width) * 2) var(--wa-color-surface-default);
inset 0 0 0 0.0625rem var(--wa-form-control-border-color),
inset 0 0 0 0.25rem var(--wa-color-surface-default);
}
.trigger--empty:before {

View File

@@ -48,21 +48,21 @@ export default class WaIcon extends WebAwesomeElement {
@state() private svg: SVGElement | HTMLTemplateResult | null = null;
/** The name of the icon to draw. Available names depend on the icon library being used. */
@property({ reflect: true }) name?: string;
@property({ cssProperty: true }) name?: string;
/**
* The family of icons to choose from. For Font Awesome Free (default), valid options include `classic` and `brands`.
* For Font Awesome Pro subscribers, valid options include, `classic`, `sharp`, `duotone`, and `brands`. Custom icon
* libraries may or may not use this property.
*/
@property({ reflect: true }) family: string;
@property({ cssProperty: true }) family: string;
/**
* The name of the icon's variant. For Font Awesome, valid options include `thin`, `light`, `regular`, and `solid` for
* the `classic` and `sharp` families. Some variants require a Font Awesome Pro subscription. Custom icon libraries
* may or may not use this property.
*/
@property({ reflect: true }) variant: string;
@property({ cssProperty: true }) variant: string;
/** Draws the icon in a fixed-width both. */
@property({ attribute: 'fixed-width', type: Boolean, reflect: true }) fixedWidth: false;
@@ -80,10 +80,11 @@ export default class WaIcon extends WebAwesomeElement {
@property() label = '';
/** The name of a registered custom icon library. */
@property({ reflect: true }) library = 'default';
@property({ cssProperty: true }) library = 'default';
connectedCallback() {
super.connectedCallback();
watchIcon(this);
}

View File

@@ -54,7 +54,6 @@ import styles from './input.css';
* @csspart suffix - The container that wraps the suffix.
*
* @cssproperty --background-color - The input's background color.
* @cssproperty --border-color - The color of the input's borders.
* @cssproperty --border-width - The width of the input's borders. Expects a single value.
* @cssproperty --box-shadow - The shadow effects around the edges of the input.
*/

View File

@@ -34,12 +34,14 @@
*/
:host([checked]) {
--indicator-color: var(--wa-form-control-activated-color);
--background-color: var(--wa-color-brand-fill-quiet);
--background-color-hover: var(--background-color);
--border-color: var(--wa-form-control-activated-color);
--text-color: var(--wa-color-brand-on-normal);
--indicator-color: var(--border-color);
--indicator-width: var(--wa-border-width-s);
box-shadow: inset 0 0 0 var(--indicator-width) var(--indicator-color);
& button {
--border-color: var(--indicator-color);
}
box-shadow:
var(--box-shadow, 0 0 transparent),
inset 0 0 0 var(--indicator-width) var(--indicator-color);
}

View File

@@ -8,7 +8,6 @@ import { HasSlotController } from '../../internal/slot.js';
import { watch } from '../../internal/watch.js';
import { WebAwesomeFormAssociatedElement } from '../../internal/webawesome-formassociated-element.js';
import nativeStyles from '../../styles/native/button.css';
import appearanceStyles from '../../styles/utilities/appearance.css';
import sizeStyles from '../../styles/utilities/size.css';
import variantStyles from '../../styles/utilities/variants.css';
import buttonStyles from '../button/button.css';
@@ -51,7 +50,7 @@ import styles from './radio-button.css';
*/
@customElement('wa-radio-button')
export default class WaRadioButton extends WebAwesomeFormAssociatedElement {
static shadowStyle = [variantStyles, appearanceStyles, sizeStyles, nativeStyles, buttonStyles, styles];
static shadowStyle = [variantStyles, sizeStyles, nativeStyles, buttonStyles, styles];
private readonly hasSlotController = new HasSlotController(this, '[default]', 'prefix', 'suffix');

View File

@@ -81,6 +81,8 @@ import styles from './select.css';
*
* @cssproperty --background-color - The background color of the select's combobox.
* @cssproperty --border-color - The border color of the select's combobox.
* @cssproperty --border-radius - The border radius of the select's combobox.
* @cssproperty --border-style - The style of the select's borders, including the listbox.
* @cssproperty --border-width - The width of the select's borders, including the listbox.
* @cssproperty --box-shadow - The shadow effects around the edges of the select's combobox.
*/

View File

@@ -38,7 +38,7 @@
}
--inset-block: calc(50% + (var(--thumb-size) / 2) + var(--tooltip-offset));
--border-block: var(--wa-tooltip-arrow-size) solid var(--wa-tooltip-background-color);
--border-block: var(--wa-tooltip-arrow-size) solid var(--wa-color-neutral-fill-loud);
@media (forced-colors: active) {
border: solid 1px transparent;

View File

@@ -41,6 +41,8 @@ import styles from './textarea.css';
*
* @cssproperty --background-color - The textarea's background color.
* @cssproperty --border-color - The color of the textarea's borders.
* @cssproperty --border-radius - The border radius of the textarea's corners.
* @cssproperty --border-style - The style of the textarea's borders.
* @cssproperty --border-width - The width of the textarea's borders.
* @cssproperty --box-shadow - The shadow effects around the edges of the textarea.
*/

View File

@@ -1,7 +1,18 @@
import type { CSSResult, CSSResultGroup, PropertyValues } from 'lit';
import type { CSSResult, CSSResultGroup, PropertyDeclaration, PropertyValues } from 'lit';
import { LitElement, isServer, unsafeCSS } from 'lit';
import { property } from 'lit/decorators.js';
import componentStyles from '../styles/shadow/component.css';
import { getComputedStyle } from './computedStyle.js';
// Augment Lit's module
declare module 'lit' {
interface PropertyDeclaration {
/**
* Indicates whether the property should reflect to a CSS custom property.
*/
cssProperty?: true | string;
}
}
export default class WebAwesomeElement extends LitElement {
constructor() {
@@ -52,6 +63,16 @@ export default class WebAwesomeElement extends LitElement {
internals: ElementInternals;
#computedStyle: CSSStyleDeclaration | null;
#setVia: Record<PropertyKey, 'css' | 'attribute' | 'js'> = {};
#setting = new Set<PropertyKey>();
connectedCallback() {
super.connectedCallback();
this.updateCSSProperties();
}
attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null) {
if (!this.#hasRecordedInitialProperties) {
(this.constructor as typeof WebAwesomeElement).elementProperties.forEach(
@@ -111,6 +132,21 @@ export default class WebAwesomeElement extends LitElement {
}
}
updated(changedProperties: PropertyValues<this>) {
super.updated(changedProperties);
let Self = this.constructor as typeof WebAwesomeElement;
if (Self.cssAttributeProperties.size > 0) {
for (let [name] of changedProperties) {
if (typeof name === 'string' && this.#setVia[name] === 'css' && !this.#setting.has(name)) {
// A property is being set via JS and its NOT because we're reflecting a CSS property
this.#setVia[name] = 'js';
}
}
}
}
/** Checks if states are supported by the element */
private hasStatesSupport(): boolean {
return Boolean(this.internals?.states);
@@ -148,4 +184,51 @@ export default class WebAwesomeElement extends LitElement {
hasCustomState(state: string): boolean {
return this.hasStatesSupport() ? this.internals.states.has(state) : false;
}
protected updateCSSProperties() {
const Self = this.constructor as typeof WebAwesomeElement;
if (Self.cssAttributeProperties.size === 0) {
return;
}
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) {
this.#setVia[name] = 'css';
this.#setting.add(name);
// @ts-ignore
this[name] = value.trim();
this.updateComplete.then(() => {
this.#setting.delete(name);
});
}
}
}
}
// Subclasses will override this
protected static cssAttributeProperties = new Map<PropertyKey, true | string>();
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 Set
this.cssAttributeProperties = new Map();
}
this.cssAttributeProperties.set(name, options.cssProperty);
}
}
}

View File

@@ -2,11 +2,12 @@ input[type='color'] {
display: block;
border-radius: calc(infinity * 1px);
background: transparent;
padding: var(--wa-form-control-border-width);
padding: 3px;
width: calc(var(--wa-form-control-height) - 2px);
height: calc(var(--wa-form-control-height) - 2px);
border: var(--wa-form-control-border-width) var(--wa-border-style) var(--wa-form-control-border-color);
border: var(--wa-border-width-s) var(--wa-border-style) var(--wa-form-control-border-color);
cursor: pointer;
margin-block-start: 3px;
forced-color-adjust: none;
&::-webkit-color-swatch-wrapper {

View File

@@ -1,45 +1,41 @@
select,
label:has(select),
:host {
/* Defaults for root element. */
--outlined-background-color: var(--wa-form-control-background-color);
--outlined-border-color: var(--wa-form-control-border-color);
--outlined-text-color: var(--wa-form-control-value-color);
:where(&) {
/* Defaults with 0 specificity.
* Do NOT reset --background-color and --border-color here so they trickle in from the appearance utils
* Instead we provide the fallback when setting
*/
--border-width: var(--wa-form-control-border-width);
--box-shadow: initial;
}
--background-color: var(--wa-form-control-background-color);
--border-color: var(--wa-form-control-border-color);
--border-radius: var(--wa-form-control-border-radius);
--border-style: var(--wa-form-control-border-style);
--border-width: var(--wa-form-control-border-width);
--box-shadow: initial;
}
select,
:host [part~='combobox'] {
background-color: var(--background-color, var(--wa-form-control-background-color));
border-color: var(--border-color, var(--wa-form-control-border-color));
border-radius: var(--wa-form-control-border-radius);
border-style: var(--wa-form-control-border-style);
background-color: var(--background-color);
border-color: var(--border-color);
border-radius: var(--border-radius);
border-style: var(--border-style);
border-width: var(--border-width);
box-shadow: var(--box-shadow);
width: 100%;
min-width: 0;
position: relative;
color: var(--wa-form-control-value-color);
cursor: pointer;
font-family: inherit;
font-size: var(--wa-size);
font-family: inherit;
font-weight: var(--wa-form-control-value-font-weight);
line-height: var(--wa-form-control-value-line-height);
min-width: 0;
overflow: hidden;
padding: var(--wa-space-smaller) var(--wa-space);
position: relative;
vertical-align: middle;
width: 100%;
overflow: hidden;
cursor: pointer;
transition:
background-color var(--wa-transition-normal),
background var(--wa-transition-normal),
border var(--wa-transition-normal),
box-shadow var(--wa-transition-normal),
color var(--wa-transition-normal),
outline var(--wa-transition-fast);
transition-timing-function: var(--wa-transition-easing);
padding: var(--wa-space-smaller) var(--wa-space);
}
/* Add ellipses to multi select options */

View File

@@ -247,13 +247,10 @@ wa-badge {
text-transform: uppercase;
}
:is(
button,
input:where([type='button'], [type='reset'], [type='submit']),
wa-button,
wa-radio-group > wa-radio-button,
.wa-button
):not([appearance='plain'], .wa-plain) {
:is(button, input:where([type='button'], [type='reset'], [type='submit']), wa-button, .wa-button):not(
[appearance='plain'],
.wa-plain
) {
--wa-transition-slow: 0;
--wa-transition-normal: 0;
--wa-transition-fast: 0;
@@ -272,8 +269,7 @@ wa-badge {
--background-color: var(--wa-color-surface-default);
}
&:not([disabled], [loading]):active,
&::part(checked) {
&:not([disabled], [loading]):active {
box-shadow: none;
transform: translateY(var(--wa-shadow-offset-y-s));
}
@@ -312,12 +308,18 @@ wa-checkbox {
}
wa-radio-group > wa-radio-button {
&::part(base):active,
&::part(checked) {
--background-color-active: var(--border-color);
--border-color-active: var(--background-color-active);
--box-shadow: var(--wa-shadow-offset-x-s) var(--wa-shadow-offset-y-s) var(--wa-shadow-blur-s) var(--border-color);
--label-color-active: var(--wa-color-surface-default);
&:active,
&[checked] {
--background-color: var(--border-color);
--background-color-hover: var(--border-color);
--border-color: var(--wa-color-neutral-border-loud);
--text-color: var(--wa-color-surface-default);
--box-shadow: none;
--label-color: var(--wa-color-surface-default);
transform: translate(var(--wa-shadow-offset-x-s), var(--wa-shadow-offset-y-s));
}
}