From 25623d686663388ef1fe427e99c9040359516fa7 Mon Sep 17 00:00:00 2001 From: Westbrook Johnson Date: Tue, 8 Oct 2019 20:45:33 -0400 Subject: [PATCH] feat(button): allow icon only buttons --- packages/button/src/button-base.ts | 21 +++++++++++++++- .../button/stories/action-button.stories.ts | 24 ++++++++++++++----- packages/button/test/button.test.ts | 15 ++++++++++++ 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/packages/button/src/button-base.ts b/packages/button/src/button-base.ts index 96bb6a324e..1af89051e4 100644 --- a/packages/button/src/button-base.ts +++ b/packages/button/src/button-base.ts @@ -32,6 +32,8 @@ export class ButtonBase extends Focusable { return !!this.querySelector('[slot="icon"]'); } + private hasLabel = false; + public get focusElement(): HTMLElement { /* istanbul ignore if */ if (!this.shadowRoot) { @@ -40,13 +42,30 @@ export class ButtonBase extends Focusable { return this.shadowRoot.querySelector('#button') as HTMLElement; } + private manageLabelSlot(e: Event): void { + const slot = e.target as HTMLSlotElement; + let assignedElements = slot.assignedElements + ? slot.assignedNodes() + : [...this.childNodes].filter((node) => { + const el = node as HTMLElement; + return !el.hasAttribute('slot'); + }); + assignedElements = assignedElements.filter((node) => { + return node.textContent ? node.textContent.trim() : false; + }); + this.hasLabel = assignedElements.length > 0; + this.requestUpdate(); + } + protected get buttonContent(): TemplateResult[] { const icon = html` `; const content = [ html` -
+
+ +
`, ]; if (!this.hasIcon) { diff --git a/packages/button/stories/action-button.stories.ts b/packages/button/stories/action-button.stories.ts index 7fca369f95..1ba06601d6 100644 --- a/packages/button/stories/action-button.stories.ts +++ b/packages/button/stories/action-button.stories.ts @@ -45,10 +45,22 @@ function renderButtonsSelected(properties: Properties): TemplateResult { `; } -storiesOf('ActionButton', module).add('Default', () => { - return renderButtonsSelected({ - quiet: false, - disabled: false, - selected: false, +storiesOf('ActionButton', module) + .add('Default', () => { + return renderButtonsSelected({ + quiet: false, + disabled: false, + selected: false, + }); + }) + .add('Icon button', () => { + return html` + + + + + + `; }); -}); diff --git a/packages/button/test/button.test.ts b/packages/button/test/button.test.ts index 8aaf1f1713..59ff9fff9e 100644 --- a/packages/button/test/button.test.ts +++ b/packages/button/test/button.test.ts @@ -57,6 +57,21 @@ describe('Button', () => { `` ); }); + it('loads default only icon', async () => { + const el = await fixture` + ); + }); it('loads default w/ an icon on the right', async () => { const el = await fixture