Skip to content

Commit

Permalink
Merge pull request #126 from phase2/feature/conditional-slot-template
Browse files Browse the repository at this point in the history
Add conditional slot template method to `outline-element`
  • Loading branch information
himerus committed Dec 2, 2021
2 parents 44c5330 + 834ccbc commit ca7cb70
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/components/base/outline-element/outline-element.ts
@@ -1,4 +1,6 @@
import { LitElement } from 'lit';
import { LitElement, TemplateResult } from 'lit';
import { html, unsafeStatic } from 'lit/static-html.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { queryAll, queryAssignedNodes, customElement } from 'lit/decorators.js';
//import componentStyles from './outline-element.base.css.lit';

Expand Down Expand Up @@ -36,4 +38,39 @@ export class OutlineElement extends LitElement {
// render(): TemplateResult {
// return html``;
// }

/**
* Create a conditional slot.
*
* Generates the slot if there is slotted content. Includes a wrapper:
* ```
* <div id="header">
* <slot name="outline-element--header"></slot>
* </div>
* ```
*/
protected _conditionalSlotTemplate({
elementName,
slotNameStub,
wrapperElementType = 'div',
ariaLabel = null,
}: {
elementName: string;
slotNameStub: string;
wrapperElementType?: string;
ariaLabel?: string | null;
}): TemplateResult | null {
const namespacedSlotName = `${elementName}--${slotNameStub}`;

return this.querySelector(`[slot="${namespacedSlotName}"]`) !== null
? html`<${unsafeStatic(
wrapperElementType
)} id="${slotNameStub}" aria-label="${ifDefined(ariaLabel)}">
<slot
name="${namespacedSlotName}"
@slotchange="${() => this.requestUpdate()}"
></slot>
</${unsafeStatic(wrapperElementType)}>`
: null;
}
}

0 comments on commit ca7cb70

Please sign in to comment.