Skip to content

Commit

Permalink
[@next] port deprecated (2.x) button for backwards compatibility (#454)
Browse files Browse the repository at this point in the history
  • Loading branch information
DRiFTy17 authored Jan 25, 2024
1 parent 69687ec commit ad38de0
Show file tree
Hide file tree
Showing 13 changed files with 818 additions and 2 deletions.
34 changes: 34 additions & 0 deletions src/dev/pages/button/button.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,40 @@
</form>
</dialog>
</section>

<section class="vert">
<h3 class="forge-typography--heading2">Deprecated (legacy) button</h3>
<forge-deprecated-button>
<button type="button">Default button</button>
</forge-deprecated-button>
<forge-deprecated-button type="outlined">
<button type="button">Outlined button</button>
</forge-deprecated-button>
<forge-deprecated-button type="unelevated">
<button type="button">Unelevated button</button>
</forge-deprecated-button>
<forge-deprecated-button type="raised">
<button type="button">Raised button</button>
</forge-deprecated-button>
<forge-deprecated-button type="outlined">
<button type="button">
<forge-icon name="favorite"></forge-icon>
<span>w/Leading Icon</span>
</button>
</forge-deprecated-button>
<forge-deprecated-button type="outlined">
<button type="button">
<span>w/Trailing icon</span>
<forge-icon name="favorite"></forge-icon>
</button>
</forge-deprecated-button>
<forge-deprecated-button type="outlined">
<a href="javascript: alert('Anchor link button works!');">
<span>w/anchor tag</span>
<forge-icon name="open_in_new"></forge-icon>
</a>
</forge-deprecated-button>
</section>
</div>

<script type="module" src="button.ts"></script>
2 changes: 1 addition & 1 deletion src/dev/pages/button/button.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
gap: 16px;
}

forge-button:not([full-width]) {
:is(forge-button,forge-deprecated-button):not([full-width]) {
width: 256px;
}

Expand Down
8 changes: 8 additions & 0 deletions src/dev/pages/button/button.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import '$src/shared';
import type { ISwitchComponent } from '@tylertech/forge/switch';
import type { IButtonComponent } from '@tylertech/forge/button';
import type { IDeprecatedButtonComponent } from '@tylertech/forge/deprecated/button';
import type { ISelectComponent } from '@tylertech/forge/select';
import { IconRegistry } from '@tylertech/forge/icon';
import { tylIconFavorite, tylIconOpenInNew } from '@tylertech/tyler-icons/standard';
import { tylIconForgeLogo } from '@tylertech/tyler-icons/custom';
import '@tylertech/forge/button';
import '@tylertech/forge/deprecated/button';
import '@tylertech/forge/label';
import './button.scss';

Expand Down Expand Up @@ -55,17 +57,22 @@ showDialogBtn.addEventListener('click', () => {
dialog.showModal();
});

const allDeprecatedButtons = Array.from(document.querySelectorAll<IDeprecatedButtonComponent>('.content forge-deprecated-button'));
const allButtons = Array.from(document.querySelectorAll<IButtonComponent>('.content forge-button'));
allButtons.forEach(btn => btn.addEventListener('click', evt => console.log('click', evt)));

const disabledToggle = document.querySelector('#opt-disabled') as ISwitchComponent;
disabledToggle.addEventListener('forge-switch-change', ({ detail: selected }) => {
allButtons.forEach(btn => btn.toggleAttribute('disabled', selected));
allDeprecatedButtons.forEach(btn => btn.toggleAttribute('disabled', selected));
});

const denseToggle = document.querySelector('#opt-dense') as ISwitchComponent;
denseToggle.addEventListener('forge-switch-change', ({ detail: selected }) => {
allButtons.forEach(btn => btn.dense = selected);
allDeprecatedButtons.forEach(btn => {
btn.type = btn.type ? btn.type.replace(/(?:-?dense)?$/, selected ? '-dense' : '') : selected ? 'dense' : '';
});
});

const pillToggle = document.querySelector('#opt-pill') as ISwitchComponent;
Expand All @@ -81,6 +88,7 @@ anchorToggle.addEventListener('forge-switch-change', ({ detail: selected }) => {
const fullWidthToggle = document.querySelector('#opt-full-width') as ISwitchComponent;
fullWidthToggle.addEventListener('forge-switch-change', ({ detail: selected }) => {
allButtons.forEach(btn => btn.fullWidth = selected);
allDeprecatedButtons.forEach(btn => btn.toggleAttribute('full-width', selected));
});

const popoverIconToggle = document.querySelector('#opt-popover-icon') as ISwitchComponent;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/core/styles/typography/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
@mixin anchor {
a:not([forge-ignore]),
.forge-typography--link {
text-decoration: underline;
text-decoration: var(--forge-typography-link-text-decoration, underline);
color: theme.variable(primary);
cursor: pointer;

Expand Down
49 changes: 49 additions & 0 deletions src/lib/deprecated/button/deprecated-button-component-delegate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { BaseComponentDelegate, IBaseComponentDelegateConfig, IBaseComponentDelegateOptions } from '../../core/delegates/base-component-delegate';
import { IDeprecatedButtonComponent } from './deprecated-button';
import { DEPRECATED_BUTTON_CONSTANTS } from './deprecated-button-constants';

export type DeprecatedButtonComponentDelegateProps = Partial<IDeprecatedButtonComponent>;
export interface IDeprecatedButtonComponentDelegateOptions extends IBaseComponentDelegateOptions {
type?: 'button' | 'submit';
text?: string;
}
export interface IDeprecatedButtonComponentDelegateConfig extends IBaseComponentDelegateConfig<IDeprecatedButtonComponent, IDeprecatedButtonComponentDelegateOptions> {}

export class DeprecatedButtonComponentDelegate extends BaseComponentDelegate<IDeprecatedButtonComponent, IDeprecatedButtonComponentDelegateOptions> {
private _buttonElement?: HTMLButtonElement;

constructor(config?: IDeprecatedButtonComponentDelegateConfig) {
super(config);
}

public override destroy(): void {
this._buttonElement = undefined;
}

public get buttonElement(): HTMLButtonElement | undefined {
return this._buttonElement;
}

protected _build(): IDeprecatedButtonComponent {
const component = document.createElement(DEPRECATED_BUTTON_CONSTANTS.elementName);

this._buttonElement = document.createElement('button');
this._buttonElement.type = this._config.options?.type || 'button';
this._buttonElement.textContent = this._config.options?.text || '';
component.appendChild(this._buttonElement);

return component;
}

public onClick(listener: (evt: MouseEvent) => void): void {
this._buttonElement?.addEventListener('click', listener);
}

public onFocus(listener: (evt: Event) => void): void {
this._buttonElement?.addEventListener('focus', evt => listener(evt));
}

public onBlur(listener: (evt: Event) => void): void {
this._buttonElement?.addEventListener('blur', evt => listener(evt));
}
}
21 changes: 21 additions & 0 deletions src/lib/deprecated/button/deprecated-button-constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { COMPONENT_NAME_PREFIX } from '../../constants';

const elementName: keyof HTMLElementTagNameMap = `${COMPONENT_NAME_PREFIX}deprecated-button`;

const attributes = {
TYPE: 'type',
DISABLED: 'disabled',
FULL_WIDTH: 'full-width'
};

const selectors = {
BUTTON: 'button,a'
};

export const DEPRECATED_BUTTON_CONSTANTS = {
elementName,
attributes,
selectors
};

export type DeprecatedButtonType = 'text' | 'raised' | 'unelevated' | 'outlined' | 'dense';
5 changes: 5 additions & 0 deletions src/lib/deprecated/button/deprecated-button.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<slot></slot>
<forge-focus-indicator part="focus-indicator"></forge-focus-indicator>
<forge-state-layer exportparts="surface:state-layer"></forge-state-layer>
</template>
128 changes: 128 additions & 0 deletions src/lib/deprecated/button/deprecated-button.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
@use '../../button/core' as *;
@use '../../state-layer';
@use '../../focus-indicator';

//
// Host
//

:host {
@include tokens;
}

:host {
@include host;

border-radius: #{token(shape)};
}

:host([hidden]) {
display: none;
}

//
// Base
//

::slotted(:is(button,a)) {
@include base;

--forge-icon-font-size: 1.25em;
}

::slotted(a) {
text-decoration: none;
--forge-typography-link-text-decoration: none;
}

forge-state-layer {
@include state-layer.provide-theme(( color: #{token(color)} ));
}

forge-focus-indicator {
@include focus-indicator.provide-theme((
color: #{token(primary-color)},
outward-offset: #{token(focus-indicator-offset)}
));
}

//
// Types
//

:host(:is(:not([type],[type*=text]))) {
::slotted(:is(button,a)) {
@include text;
}
}

:host(:is([type*=unelevated],[type*=raised])) {
::slotted(:is(button,a)) {
@include filled;
}

forge-state-layer {
@include state-layer.provide-theme(( color: #{token(filled-color)} ));
}
}

:host([type*=raised]) {
::slotted(:is(button,a)) {
@include raised;
}
}

:host([type*=outlined]) {
::slotted(:is(button,a)) {
@include outlined;
}
}

//
// Full width
//

:host([full-width]) {
width: 100%;
}


//
// Dense
//

:host(:is([dense],[type*=dense])) {
::slotted(:is(button,a)) {
@include dense;
}
}

//
// Disabled
//

:host([disabled]) {
@include host-disabled;

::slotted(button[disabled]) {
@include disabled;
}
}

:host([type*=outlined][disabled]) {
::slotted(button[disabled]) {
@include outlined-disabled;
}
}

:host(:is([type*=unelevated],[type*=raised])[disabled]) {
::slotted(button[disabled]) {
@include filled-disabled;
}
}

:host([type*=raised][disabled]) {
::slotted(button[disabled]) {
@include raised-disabled;
}
}
Loading

0 comments on commit ad38de0

Please sign in to comment.