Skip to content

Commit 5921f3e

Browse files
authored
Button slot detection (#976)
* reimplement slot detection; remove broken visually hidden logic * update example now that wa-visually-hidden is not a component * cleanup
1 parent 35338ef commit 5921f3e

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

docs/docs/components/button-group.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ Create a split button using a button and a dropdown. Use a [visually hidden](/do
186186
<wa-button-group label="Example Button Group">
187187
<wa-button variant="brand">Save</wa-button>
188188
<wa-dropdown placement="bottom-end">
189-
<wa-button slot="trigger" variant="brand" caret>
190-
<span class="wa-visually-hidden">More options</span>
189+
<wa-button slot="trigger" variant="brand">
190+
<wa-icon name="chevron-down" label="More options"></wa-icon>
191191
</wa-button>
192192
<wa-menu>
193193
<wa-menu-item>Save</wa-menu-item>

src/components/button/button.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { classMap } from 'lit/directives/class-map.js';
33
import { ifDefined } from 'lit/directives/if-defined.js';
44
import { html, literal } from 'lit/static-html.js';
55
import { WaInvalidEvent } from '../../events/invalid.js';
6+
import { HasSlotController } from '../../internal/slot.js';
67
import { MirrorValidator } from '../../internal/validators/mirror-validator.js';
78
import { watch } from '../../internal/watch.js';
89
import { WebAwesomeFormAssociatedElement } from '../../internal/webawesome-form-associated-element.js';
@@ -61,11 +62,11 @@ export default class WaButton extends WebAwesomeFormAssociatedElement {
6162
}
6263

6364
assumeInteractionOn = ['click'];
65+
private readonly hasSlotController = new HasSlotController(this, '[default]', 'prefix', 'suffix');
6466
private readonly localize = new LocalizeController(this);
6567

6668
@query('.wa-button') button: HTMLButtonElement | HTMLLinkElement;
6769

68-
@state() visuallyHiddenLabel = false;
6970
@state() invalid = false;
7071
@property() title = ''; // make reactive to pass through
7172

@@ -184,15 +185,6 @@ export default class WaButton extends WebAwesomeFormAssociatedElement {
184185
this.dispatchEvent(new WaInvalidEvent());
185186
}
186187

187-
private handleLabelSlotChange(event: Event) {
188-
// If the only thing slotted in is a visually hidden element, we consider it a visually hidden label and apply a
189-
// class so we can adjust styles accordingly.
190-
const elements = (event.target as HTMLSlotElement).assignedElements({ flatten: true });
191-
if (elements.length === 1 && elements[0].localName === 'wa-visually-hidden') {
192-
this.visuallyHiddenLabel = true;
193-
}
194-
}
195-
196188
private isButton() {
197189
return this.href ? false : true;
198190
}
@@ -243,7 +235,9 @@ export default class WaButton extends WebAwesomeFormAssociatedElement {
243235
disabled: this.disabled,
244236
loading: this.loading,
245237
rtl: this.localize.dir() === 'rtl',
246-
'visually-hidden-label': this.visuallyHiddenLabel,
238+
'has-label': this.hasSlotController.test('[default]'),
239+
'has-prefix': this.hasSlotController.test('prefix'),
240+
'has-suffix': this.hasSlotController.test('suffix'),
247241
})}
248242
?disabled=${ifDefined(isLink ? undefined : this.disabled)}
249243
type=${ifDefined(isLink ? undefined : this.type)}
@@ -261,7 +255,7 @@ export default class WaButton extends WebAwesomeFormAssociatedElement {
261255
@click=${this.handleClick}
262256
>
263257
<slot name="prefix" part="prefix" class="prefix"></slot>
264-
<slot part="label" class="label" @slotchange=${this.handleLabelSlotChange}></slot>
258+
<slot part="label" class="label"></slot>
265259
<slot name="suffix" part="suffix" class="suffix"></slot>
266260
${
267261
this.caret

0 commit comments

Comments
 (0)