-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new split-button component (#430)
- Loading branch information
Showing
25 changed files
with
762 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<div> | ||
<h3 class="forge-typography--heading2">Common</h3> | ||
<forge-split-button> | ||
<forge-button class="primary-action">Send</forge-button> | ||
<forge-menu id="split-menu"> | ||
<forge-button popover-icon></forge-button> | ||
</forge-menu> | ||
</forge-split-button> | ||
</div> | ||
|
||
<div> | ||
<h3 class="forge-typography--heading2">Multiple</h3> | ||
<forge-split-button> | ||
<forge-button class="primary-action">Button one</forge-button> | ||
<forge-button class="primary-action">Button two</forge-button> | ||
<forge-button class="primary-action">Button three</forge-button> | ||
<forge-button class="primary-action">Button four</forge-button> | ||
</forge-split-button> | ||
</div> | ||
|
||
<script type="module" src="split-button.ts"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<%- | ||
include('./src/partials/page.ejs', { | ||
page: { | ||
title: 'Split button', | ||
includePath: './pages/split-button/split-button.ejs', | ||
options: [ | ||
{ | ||
type: 'select', | ||
label: 'Variant', | ||
id: 'opt-variant', | ||
defaultValue: 'text', | ||
options: [ | ||
{ value: 'text', label: 'Text (default)' }, | ||
{ value: 'flat', label: 'Flat' }, | ||
{ value: 'raised', label: 'Raised' }, | ||
{ value: 'outlined', label: 'Outlined' } | ||
] | ||
}, | ||
{ type: 'switch', label: 'Disabled', id: 'opt-disabled' }, | ||
{ type: 'switch', label: 'Dense', id: 'opt-dense' }, | ||
{ type: 'switch', label: 'Pill', id: 'opt-pill' } | ||
] | ||
} | ||
}) | ||
%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
forge-split-button[variant]:not([variant=text]) { | ||
.primary-action { | ||
min-width: 100px; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import '$src/shared'; | ||
import '@tylertech/forge/split-button'; | ||
import { IconRegistry } from '@tylertech/forge/icon'; | ||
import type { ISplitButtonComponent, SplitButtonVariant } from '@tylertech/forge/split-button'; | ||
import type { ISelectComponent } from '@tylertech/forge/select'; | ||
import type { ISwitchComponent } from '@tylertech/forge/switch'; | ||
import type { IMenuComponent } from '@tylertech/forge/menu'; | ||
import { tylIconArrowDropDown, tylIconScheduleSend, tylIconDeleteOutline, tylIconBookmarkBorder } from '@tylertech/tyler-icons/standard'; | ||
import './split-button.scss'; | ||
|
||
IconRegistry.define([tylIconArrowDropDown, tylIconScheduleSend, tylIconDeleteOutline, tylIconBookmarkBorder]); | ||
|
||
const splitMenu = document.querySelector('#split-menu') as IMenuComponent; | ||
splitMenu.options = [ | ||
{ label: 'Schedule send', value: 'schedule', leadingIcon: 'schedule_send', leadingIconType: 'component' }, | ||
{ label: 'Delete', value: 'delete', leadingIcon: 'delete_outline', leadingIconType: 'component' }, | ||
{ label: 'Save draft', value: 'save', leadingIcon: 'bookmark_border', leadingIconType: 'component' } | ||
]; | ||
|
||
const variantSelect = document.querySelector('#opt-variant') as ISelectComponent; | ||
variantSelect.addEventListener('change', ({ detail: variant }: CustomEvent<SplitButtonVariant>) => { | ||
getSplitButtons().forEach(splitButton => splitButton.variant = variant); | ||
}); | ||
|
||
const disabledToggle = document.querySelector('#opt-disabled') as ISwitchComponent; | ||
disabledToggle.addEventListener('forge-switch-change', ({ detail: selected }) => { | ||
getSplitButtons().forEach(splitButton => splitButton.disabled = selected); | ||
}); | ||
|
||
const denseToggle = document.querySelector('#opt-dense') as ISwitchComponent; | ||
denseToggle.addEventListener('forge-switch-change', ({ detail: selected }) => { | ||
getSplitButtons().forEach(splitButton => splitButton.dense = selected); | ||
}); | ||
|
||
const pillToggle = document.querySelector('#opt-pill') as ISwitchComponent; | ||
pillToggle.addEventListener('forge-switch-change', ({ detail: selected }) => { | ||
getSplitButtons().forEach(splitButton => splitButton.pill = selected); | ||
}); | ||
|
||
function getSplitButtons(): ISplitButtonComponent[] { | ||
return Array.from(document.querySelectorAll<ISplitButtonComponent>('forge-split-button')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
@use 'sass:map'; | ||
@use '../../utils'; | ||
@use '../../border'; | ||
@use '../button/tokens' as button; | ||
|
||
$tokens: ( | ||
min-width: utils.module-val(split-button, min-width, 0), | ||
gap: utils.module-val(split-button, gap, border.variable(thin)), | ||
|
||
focus-indicator-offset: utils.module-val(split-button, focus-indicator-offset, button.get(focus-indicator-offset)), | ||
focus-indicator-divider-offset: utils.module-ref(split-button, focus-indicator-divider-offset, gap) | ||
) !default; | ||
|
||
@function get($key) { | ||
@return map.get($tokens, $key); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
:host { | ||
display: inline-block; | ||
display: inline-flex; | ||
} | ||
|
||
:host([hidden]) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@use './token-utils' as *; | ||
|
||
@mixin configuration { | ||
@include tokens; | ||
|
||
#{declare(focus-indicator-offset-adjusted)}: calc(#{token(focus-indicator-offset)} + #{token(focus-indicator-divider-offset)} * 2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
@use '../core/styles/tokens/split-button/tokens'; | ||
@use '../core/styles/tokens/token-utils'; | ||
|
||
$_module: split-button; | ||
$_tokens: tokens.$tokens; | ||
|
||
@mixin provide-theme($theme) { | ||
@include token-utils.provide-theme($_module, $_tokens, $theme); | ||
} | ||
|
||
@function token($name, $type: token) { | ||
@return token-utils.token($_module, $_tokens, $name, $type); | ||
} | ||
|
||
@function declare($token) { | ||
@return token-utils.declare($_module, $token); | ||
} | ||
|
||
@mixin override($token, $token-or-value, $type: token) { | ||
@include token-utils.override($_module, $_tokens, $token, $token-or-value, $type); | ||
} | ||
|
||
@mixin tokens($includes: null, $excludes: null) { | ||
@include token-utils.tokens($_module, $_tokens, $includes, $excludes); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"$schema": "../../../node_modules/@tylertech/forge-cli/config/build-schema.json", | ||
"extends": "../build.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@forward './configuration'; | ||
@forward './token-utils' show provide-theme; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { defineCustomElement } from '@tylertech/forge-core'; | ||
|
||
import { SplitButtonComponent } from './split-button'; | ||
|
||
export * from './split-button-adapter'; | ||
export * from './split-button-constants'; | ||
export * from './split-button-foundation'; | ||
export * from './split-button'; | ||
|
||
export function defineSplitButtonComponent(): void { | ||
defineCustomElement(SplitButtonComponent); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { ButtonVariant, BUTTON_CONSTANTS, IButtonComponent } from '../button'; | ||
import { BaseAdapter, IBaseAdapter } from '../core/base/base-adapter'; | ||
import { ISplitButtonComponent } from './split-button'; | ||
|
||
export interface ISplitButtonAdapter extends IBaseAdapter { | ||
setVariant(variant: ButtonVariant): void; | ||
setDisabled(value: boolean): void; | ||
setDense(value: boolean): void; | ||
setPill(value: boolean): void; | ||
startButtonObserver(): void; | ||
destroyButtonObserver(): void; | ||
} | ||
|
||
export class SplitButtonAdapter extends BaseAdapter<ISplitButtonComponent> implements ISplitButtonAdapter { | ||
private _buttonChangeObserver: MutationObserver | undefined; | ||
|
||
constructor(component: ISplitButtonComponent) { | ||
super(component); | ||
} | ||
|
||
public startButtonObserver(): void { | ||
// This observer is used to keep the buttons in sync with the split button state when they are added to DOM | ||
this._buttonChangeObserver = new MutationObserver(mutations => { | ||
// Find all `<forge-button>` elements that are contained within the added nodes | ||
const addedButtons = mutations.reduce((buttons, { addedNodes }) => { | ||
const addedButtonNodes = Array.from(addedNodes) | ||
.filter(node => node.nodeType === Node.ELEMENT_NODE) | ||
.map((node: HTMLElement) => { | ||
if (node.nodeName.toLowerCase() === BUTTON_CONSTANTS.elementName) { | ||
return node; | ||
} | ||
return node.querySelector(BUTTON_CONSTANTS.elementName); | ||
}) | ||
.filter(node => !!node) as IButtonComponent[]; | ||
return buttons.concat(addedButtonNodes); | ||
}, [] as IButtonComponent[]); | ||
|
||
if (!addedButtons.length) { | ||
return; | ||
} | ||
|
||
addedButtons.forEach(button => { | ||
button.variant = this._component.variant; | ||
button.disabled = this._component.disabled; | ||
button.dense = this._component.dense; | ||
}); | ||
|
||
this.setPill(this._component.pill); | ||
}); | ||
this._buttonChangeObserver.observe(this._component, { childList: true, subtree: true }); | ||
} | ||
|
||
public destroyButtonObserver(): void { | ||
this._buttonChangeObserver?.disconnect(); | ||
this._buttonChangeObserver = undefined; | ||
} | ||
|
||
public setVariant(variant: ButtonVariant): void { | ||
const buttons = this._getButtons(); | ||
buttons.forEach(button => button.variant = variant); | ||
} | ||
|
||
public setDisabled(value: boolean): void { | ||
const buttons = this._getButtons(); | ||
buttons.forEach(button => button.disabled = value); | ||
} | ||
|
||
public setDense(value: boolean): void { | ||
const buttons = this._getButtons(); | ||
buttons.forEach(button => button.dense = value); | ||
} | ||
|
||
public setPill(value: boolean): void { | ||
const buttons = this._getButtons(); | ||
|
||
// First we reset all the middle buttons to not be pill buttons | ||
if (buttons.length > 2) { | ||
Array.from(buttons) | ||
.slice(1, buttons.length - 1) | ||
.filter(({ pill }) => pill) | ||
.forEach(button => button.pill = false); | ||
} | ||
|
||
// Only the first and last buttons need to be pill shaped | ||
const firstButton = buttons[0]; | ||
if (firstButton) { | ||
firstButton.pill = value; | ||
} | ||
|
||
const lastButton = buttons[buttons.length - 1]; | ||
if (lastButton) { | ||
lastButton.pill = value; | ||
} | ||
} | ||
|
||
private _getButtons(): NodeListOf<IButtonComponent> { | ||
return this._component.querySelectorAll(BUTTON_CONSTANTS.elementName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { ButtonVariant } from '../button'; | ||
import { COMPONENT_NAME_PREFIX } from '../constants'; | ||
|
||
const elementName: keyof HTMLElementTagNameMap = `${COMPONENT_NAME_PREFIX}split-button`; | ||
|
||
const attributes = { | ||
VARIANT: 'variant', | ||
DISABLED: 'disabled', | ||
DENSE: 'dense', | ||
PILL: 'pill' | ||
}; | ||
|
||
export const SPLIT_BUTTON_CONSTANTS = { | ||
elementName, | ||
attributes | ||
}; | ||
|
||
export const DEFAULT_VARIANT = 'text'; | ||
|
||
export type SplitButtonVariant = Extract<ButtonVariant, 'flat' | 'raised' | 'outlined' | 'text'>; |
Oops, something went wrong.