Skip to content

Commit

Permalink
feat: dropdown component (#333)
Browse files Browse the repository at this point in the history
* chore: dropdown popover

* chore: improvements on bl-button

* chore: added autoUpdate at popover

* chore: added tests

* chore: add dropdown button stories

* chore(dropdown): styling improvements

* fix(dropdown): new line before custom variable

* chore(dropdown): add play functions at story to support chromatic

* chore(dropdown): add warning to user if dropdown-item has wrong parents

* chore(dropdown): improvements on variable naming

* docs(dropdown): improvements on jsdoc

* chore(dropdown): improvements on variable naming

* fix(dropdown): improvements on dropdown-group css

* fix(dropdown): typos

* fix(dropdown): tests with custom event

* fix(dropdown): variable naming

* fix(dropdown): improvements on variable naming and a11y

* chore(dropdown): add keyboard navigation

* fix(dropdown): test

* fix(dropdown): test
  • Loading branch information
leventozen authored Dec 19, 2022
1 parent ce67b45 commit a5ce174
Show file tree
Hide file tree
Showing 18 changed files with 1,091 additions and 15 deletions.
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = {
'radio',
'dialog',
'drawer',
'dropdown',
],
],
},
Expand Down
3 changes: 3 additions & 0 deletions src/baklava.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ export { default as BlTab } from './components/tab-group/tab/bl-tab';
export { default as BlTabGroup } from './components/tab-group/bl-tab-group';
export { default as BlTabPanel } from './components/tab-group/tab-panel/bl-tab-panel';
export { default as BlTooltip } from './components/tooltip/bl-tooltip';
export { default as BlDropdown } from './components/dropdown/bl-dropdown';
export { default as BlDropdownItem } from './components/dropdown/item/bl-dropdown-item';
export { default as BlDropdownGroup } from './components/dropdown/group/bl-dropdown-group';
export { getIconPath, setIconPath } from './utilities/asset-paths';
32 changes: 30 additions & 2 deletions src/components/button/bl-button.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

display: flex;
gap: var(--margin-icon);
justify-content: center;
justify-content: var(--bl-button-justify, 'center');
align-items: center;
box-sizing: border-box;
width:100%;
Expand Down Expand Up @@ -118,7 +118,7 @@
}

:host([variant='tertiary'][disabled]) .button {
--main-color:transparent;
--main-color: transparent;
}

:host([variant='secondary']:hover:not([disabled])) .button {
Expand All @@ -130,3 +130,31 @@
--content-color: var(--main-hover-color);
--bg-color: var(--text-hover-color);
}

:host([dropdown]) .open {
display: none;
}

:host([dropdown]) .active .open {
display: inline-block;
}

:host([dropdown]) .active .close {
display: none;
}

:host .active.button {
--bg-color: var(--main-hover-color);
--border-color: var(--main-hover-color);
}

:host([variant='secondary']) .active.button {
--content-color: var(--bl-color-content-primary-contrast);
--bg-color: var(--main-hover-color);
}

:host([variant='tertiary']) .active.button {
--content-color: var(--main-color);
--bg-color: var(--bl-color-tertiary);
--border-color: transparent;
}
46 changes: 38 additions & 8 deletions src/components/button/bl-button.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CSSResultGroup, html, LitElement, TemplateResult } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { customElement, property, state, query } from 'lit/decorators.js';
import { classMap } from 'lit/directives/class-map.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { event, EventDispatcher } from '../../utilities/event';
Expand Down Expand Up @@ -78,11 +78,43 @@ export default class BlButton extends LitElement {
@property({ type: String })
type: 'submit' | null;

/**
* Sets button type to dropdown
*/
@property({ type: Boolean })
dropdown = false;

/**
* Active state
*/
@state({})
active = false;

@query('.button')
private button: HTMLAnchorElement | HTMLButtonElement;

/**
* Fires when button clicked
*/
@event('bl-click') private onClick: EventDispatcher<string>;

private get _isActive() {
return this.active;
}

private caretTemplate(): TemplateResult {
return html` <bl-icon class="open" name="arrow_up"></bl-icon>
<bl-icon class="close" name="arrow_down"></bl-icon>`;
}

private _handleClick() {
this.onClick('Click event fired!');
}

focus() {
this.button.focus();
}

get _hasIconSlot() {
return this.querySelector(':scope > [slot="icon"]') !== null;
}
Expand All @@ -109,10 +141,12 @@ export default class BlButton extends LitElement {
const isAnchor = !!this.href;
const icon = this.icon ? html`<bl-icon name=${this.icon}></bl-icon>` : '';
const slots = html`<slot name="icon">${icon}</slot> <span class="label"><slot></slot></span>`;
const caret = this.dropdown ? this.caretTemplate() : '';
const classes = classMap({
'button': true,
'has-icon': this.icon || this._hasIconSlot,
'has-content': this._hasDefaultSlot,
'active': !isAnchor && this._isActive,
});

return isAnchor
Expand All @@ -123,22 +157,18 @@ export default class BlButton extends LitElement {
href=${ifDefined(this.href)}
target=${ifDefined(this.target)}
role="button"
>${slots}</a
>`
>${slots}
</a>`
: html`<button
class=${classes}
aria-disabled="${ifDefined(this.disabled)}"
aria-label="${ifDefined(this.label)}"
?disabled=${this.disabled}
@click="${this._handleClick}"
>
${slots}
${slots} ${caret}
</button>`;
}

private _handleClick() {
this.onClick('Click event fired!');
}
}

declare global {
Expand Down
46 changes: 46 additions & 0 deletions src/components/dropdown/bl-dropdown.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
:host {
position: relative;
display: inline-block;
}

.popover {
--left: 0;
--top: 0;
--border-color: var(--bl-color-primary);

position: fixed;

/* FIXME: Use z-index variable */
z-index: 1;
display: none;
flex-direction: column;
align-items: flex-start;
padding: var(--bl-size-m);
gap: var(--bl-size-xs);
overflow-y: auto;
background: var(--bl-color-primary-background);
border: 1px solid var(--border-color);

/* FIXME: Use variables */
box-shadow: 0 10px 15px -8px #27314226;
border-radius: var(--bl-size-3xs);
left: var(--left);
top: var(--top);
box-sizing: border-box;
}

:host([kind='neutral']) .popover {
--border-color: var(--bl-color-secondary);
}

:host([kind='success']) .popover {
--border-color: var(--bl-color-success);
}

:host([kind='danger']) .popover {
--border-color: var(--bl-color-danger);
}

.visible {
display: flex;
}
147 changes: 147 additions & 0 deletions src/components/dropdown/bl-dropdown.stories.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { html } from 'lit';
import { ifDefined } from 'lit/directives/if-defined.js';
import { styleMap } from 'lit/directives/style-map.js';
import { Meta, Canvas, ArgsTable, Story, Preview, Source } from '@storybook/addon-docs';
import { userEvent } from '@storybook/testing-library';

<Meta
title="Components/Dropdown Button"
component="bl-dropdown"
argTypes={{
label: {
control: {
type: 'text'
}
},
variant: {
options: ['primary', 'secondary', 'tertiary'],
default: 'primary',
control: { type: 'select' }
},
kind: {
options: ['default', 'neutral', 'success', 'danger'],
default: 'default',
control: { type: 'select' }
},
disabled: {
control: 'boolean'
},
size: {
options: ['large', 'medium', 'small'],
control: { type: 'select' }
}
}}
/>

export const dropdownOpener = async ({ canvasElement }) => {
const dropdown = canvasElement?.querySelector('bl-dropdown')
if(dropdown.shadowRoot) {
const button = dropdown.shadowRoot.querySelector('bl-button')
await userEvent.click(button);
}
}

export const SingleDropdownButtonTemplate = (args) => html`<bl-dropdown
variant=${ifDefined(args.variant)}
kind=${ifDefined(args.kind)}
size=${ifDefined(args.size)}
label="${ifDefined(args.label)}"
?disabled=${args.disabled}
style=${ifDefined(args.styles ? styleMap(args.styles) : undefined)}
><bl-dropdown-group caption="Caption">
<bl-dropdown-item>${args.content || 'Action 1'}</bl-dropdown-item>
<bl-dropdown-item>Action 2</bl-dropdown-item>
</bl-dropdown-group>
<bl-dropdown-item>Action 3</bl-dropdown-item>
<bl-dropdown-item icon="info">Action 4</bl-dropdown-item>
<bl-dropdown-item>Action 5</bl-dropdown-item>
<bl-dropdown-group caption="Caption">
<bl-dropdown-item icon="heart">Action 6</bl-dropdown-item>
<bl-dropdown-item>Action 7</bl-dropdown-item>
</bl-dropdown-group>
</bl-dropdown>`

export const Template = (args) => html`
${SingleDropdownButtonTemplate({...args})}
${SingleDropdownButtonTemplate({variant: 'secondary', ...args})}
${SingleDropdownButtonTemplate({variant: 'tertiary', ...args})}`

export const ButtonTypes = (args) => html`
${SingleDropdownButtonTemplate({...args})}
${SingleDropdownButtonTemplate({kind: 'neutral', ...args})}
${SingleDropdownButtonTemplate({kind: 'success', ...args})}
${SingleDropdownButtonTemplate({kind: 'danger', ...args})}`

export const SizesTemplate = (args) => html`
${SingleDropdownButtonTemplate({size: 'large', ...args})}
${SingleDropdownButtonTemplate({size: 'medium', ...args})}
${SingleDropdownButtonTemplate({size: 'small', ...args})}`

# Dropdown Button

Dropdown Button is used to display a list of actions.

<bl-alert variant="warning" icon caption="Note">Inline styles in examples are only for **demo purposes**. Use regular CSS classes or tag selectors to set styles.</bl-alert>

## Dropdown Button Variants

Dropdown Button has the same variants ([Primary](/docs/components-button--primary-buttons), [Secondary](/docs/components-button--secondary-buttons) and [Tertiary](/docs/components-button--tertiary-buttons)) with the [Button](/docs/components-button--variants).
Every variant represents the importance of the actions inside it.

<Canvas>
<Story name="Variants" play={dropdownOpener}>
{Template.bind({})}
</Story>
</Canvas>

## Dropdown Button Kinds

Dropdown Button has the same kinds as the button has.
Every kind indicates a state of the dropdown buttons. It can has 4 different "kind"s. `default`, `neutral`, `success` and `danger`.

<Canvas>
<Story name="Kinds" play={dropdownOpener}>
{ButtonTypes.bind({})}
</Story>
</Canvas>

## Dropdown Button Sizes

We have 3 sizes of dropdown buttons: **Large**, **Medium**, **Small**. Default size is **Medium**.

<Canvas>
<Story name="Dropdown Button Sizes" play={dropdownOpener}>
{SizesTemplate.bind({})}
</Story>
</Canvas>

If dropdown button has an action with a long text that can not fit in a single line, popover will be automatically widen to the right side of the dropdown button.

<Canvas>
<Story name="Automatic Left Align" args={{ content: 'Action with very long text' }} play={dropdownOpener}>
{SingleDropdownButtonTemplate.bind({})}
</Story>
</Canvas>

## Disabling Dropdown Buttons

We have 2 types of disabled dropdown buttons: Disable version of Primary and Secondary buttons is the same.

<Canvas columns={1}>
<Story name="Disabling Dropdown Buttons" args={{ disabled: true }} play={dropdownOpener}>
{SizesTemplate.bind({})}
</Story>
</Canvas>

Whereas Tertiary buttons keep their transparent backgrounds.

<Canvas columns={1}>
<Story name="Disabling Tertiary Dropdown Buttons" args={{ disabled: true, variant:"tertiary" }} play={dropdownOpener}>
{SizesTemplate.bind({})}
</Story>
</Canvas>


## Reference

<ArgsTable of="bl-dropdown" />
Loading

0 comments on commit a5ce174

Please sign in to comment.