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

Conversation

damontgomery
Copy link
Contributor

@damontgomery damontgomery commented Oct 20, 2021

This is a feature we use on an internal project.

  • Only renders the wrapper / slot when there is content
  • Works when the slot content changes
  • Supports other wrappers like ul if needed
  • Supports accessible aria-label if set

I've found styling the wrappers to be more helpful than the slot content itself in most cases. For alignment / basic text styles. Even if the slotted content is expected to be there, the other features seem nice.

I don't always use it when the slot wrapper is more complex / needs siblings, etc. But most of the time, I just use this.

In the codebase, we use this pattern:

import { html, TemplateResult, CSSResultGroup } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import componentStyles from './example-video.css.lit';
import { OutlineElement } from '../../base/outline-element/outline-element';

const elementName = 'example-video';

export const videoPositions = ['left', 'right'] as const;
export type VideoPosition = typeof videoPositions[number];

export interface ExampleVideoInterface extends HTMLElement {
  videoPosition: VideoPosition;
}

/**
 * The Example video component
 *
 * @element example-video
 * @slot example-video--video - video
 * @slot example-video--heading - heading
 * @slot example-video--content - content
 * @slot example-video--footer - footer
 */
@customElement(elementName)
export class ExampleVideo
  extends OutlineElement
  implements ExampleVideoInterface
{
  static styles: CSSResultGroup = [componentStyles];

  @property({ type: String, reflect: true, attribute: 'video-position' })
  videoPosition: VideoPosition = 'left';

  render(): TemplateResult {
    return html`
      <div id="video-wrapper">
        ${this._conditionalSlotTemplate({
          elementName,
          slotNameStub: 'video',
        })}
        <div id="content-wrapper">
          ${this._conditionalSlotTemplate({
            elementName,
            slotNameStub: 'heading',
          })}
          ${this._conditionalSlotTemplate({
            elementName,
            slotNameStub: 'content',
          })}
          ${this._conditionalSlotTemplate({
            elementName,
            slotNameStub: 'footer',
          })}
        </div>
      </div>
    `;
  }
}

@damontgomery damontgomery changed the title feat(conditional-slot-template): add to outline element Add conditional slot template method to outline-element Oct 20, 2021
@damontgomery damontgomery force-pushed the feature/conditional-slot-template branch from e0c6dd5 to 382cbc8 Compare October 20, 2021 14:35
@damontgomery damontgomery force-pushed the feature/conditional-slot-template branch from 382cbc8 to 834ccbc Compare October 20, 2021 15:05
@himerus himerus merged commit ca7cb70 into next Dec 2, 2021
@himerus himerus deleted the feature/conditional-slot-template branch December 2, 2021 01:55
Copy link
Contributor

@himerus himerus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to convert this PR description into documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants