Fix slot listeners

pull/189/head
Cory LaViska 2020-08-27 17:14:47 -04:00
rodzic f4c2611575
commit 6b9a07eab8
5 zmienionych plików z 17 dodań i 0 usunięć

Wyświetl plik

@ -6,6 +6,7 @@
- Fixed a bug where options where `sl-select` options weren't always visible or scrollable
- Fixed a bug where setting `null` on `sl-input`, `sl-textarea`, or `sl-select` would throw an error
- Fixed a bug where `role` was on the wrong element and aria attribute weren't explicit in `sl-checkbox`, `sl-switch`, and `sl-radio`
- Fixed a bug where dynamically adding/removing a slot wouldn't work as expected in `sl-card`, `sl-dialog`, and `sl-drawer`
- Optimized `hasSlot` utility by using a simpler selector
## 2.0.0-beta.16

Wyświetl plik

@ -178,3 +178,13 @@ To expose custom properties for a component, define them in the `:host` block an
display: inline-block;
}
```
### Conditional Slots
When a component relies on the presence of a slot to do something, don't assume the initial state of the component is its permanent state. Users can add and remove slotted content anytime, and components should be aware of this.
- Create an `updateSlots` method that uses `hasSlot` (imported from `src/utilities/slot.ts`) to check for the required slot(s)
- Listen on the host element for the `slotchange` event and run `updateSlots`
- Don't conditionally render any slots — always use `hidden` or `display: none` so the slot exists in the DOM
See the source of card, dialog, or drawer for examples.

Wyświetl plik

@ -29,6 +29,10 @@ export class Card {
@State() hasImage = false;
@State() hasHeader = false;
connectedCallback() {
this.updateSlots = this.updateSlots.bind(this);
}
componentWillLoad() {
this.updateSlots();
this.host.shadowRoot.addEventListener('slotchange', this.updateSlots);

Wyświetl plik

@ -77,6 +77,7 @@ export class Dialog {
this.handleTransitionEnd = this.handleTransitionEnd.bind(this);
this.handleKeyDown = this.handleKeyDown.bind(this);
this.handleOverlayClick = this.handleOverlayClick.bind(this);
this.updateSlots = this.updateSlots.bind(this);
}
componentWillLoad() {

Wyświetl plik

@ -85,6 +85,7 @@ export class Drawer {
this.handleDocumentFocusIn = this.handleDocumentFocusIn.bind(this);
this.handleKeyDown = this.handleKeyDown.bind(this);
this.handleOverlayClick = this.handleOverlayClick.bind(this);
this.updateSlots = this.updateSlots.bind(this);
}
componentWillLoad() {