Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add conditional slot template method to outline-element #126

Merged
merged 1 commit into from Dec 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}
}