Skip to content

Commit

Permalink
refactor(button): add focused part
Browse files Browse the repository at this point in the history
  • Loading branch information
SisIvanova committed Apr 26, 2024
1 parent d7c0e71 commit 0730732
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 69 deletions.
Expand Up @@ -12,7 +12,6 @@ $group-item-border-thickness: rem(1px);
border-width: $group-item-border-thickness;
padding-inline: pad-inline(rem(8px), rem(10px), rem(12px));
padding-block: pad-block(rem(2px), rem(4px), rem(7px));
min-width: var(--button-min-width);

&::before {
position: absolute;
Expand Down
Expand Up @@ -23,6 +23,7 @@ $theme: $material;
--igx-icon-size: #{$icon-size};

min-height: $material-flat-btn-size;
min-width: var(--button-min-width);
color: var-get($theme, 'item-text-color');
background: var-get($theme, 'item-background');
padding-inline: rem(16px);
Expand Down
Expand Up @@ -5,5 +5,4 @@ $theme: $bootstrap;

:host {
--component-size: var(--ig-size, #{var-get($theme, 'default-size')});
--button-min-width: #{rem(42px)};
}
Expand Up @@ -5,7 +5,7 @@ $theme: $material;

:host {
--component-size: var(--ig-size, #{var-get($theme, 'default-size')});
--button-min-width: #{rem(50px)};
--button-min-width: #{rem(42px)};
}

[part~='group'] {
Expand Down
Expand Up @@ -5,4 +5,5 @@ $theme: $indigo;

:host {
--component-size: var(--ig-size, #{var-get($theme, 'default-size')});
--button-min-width: #{rem(32px)};
}
31 changes: 28 additions & 3 deletions src/components/button/button-base.ts
@@ -1,11 +1,12 @@
import { LitElement, type TemplateResult, html, nothing } from 'lit';
import { property, query } from 'lit/decorators.js';
import { property, query, state } from 'lit/decorators.js';
import { ifDefined } from 'lit/directives/if-defined.js';

import { EventEmitterMixin } from '../common//mixins/event-emitter.js';
import { blazorDeepImport } from '../common/decorators/blazorDeepImport.js';
import type { Constructor } from '../common/mixins/constructor.js';
import { SizableMixin } from '../common/mixins/sizable.js';
import { partNameMap } from '../common/util.js';

export interface IgcButtonEventMap {
igcFocus: CustomEvent<void>;
Expand All @@ -29,6 +30,9 @@ export abstract class IgcButtonBaseComponent extends SizableMixin(
@query('[part="base"]', true)
private _nativeButton!: HTMLButtonElement;

@state()
protected focused = false;

/* alternateName: displayType */
/**
* The type of the button. Defaults to `button`.
Expand Down Expand Up @@ -90,6 +94,7 @@ export abstract class IgcButtonBaseComponent extends SizableMixin(
constructor() {
super();
this.__internals = this.attachInternals();
this.addEventListener('keyup', this.handleKeyUp);
this.size = 'medium';
}

Expand All @@ -116,6 +121,7 @@ export abstract class IgcButtonBaseComponent extends SizableMixin(

protected handleBlur() {
this.emitEvent('igcBlur');
this.focused = false;
}

protected handleClick() {
Expand All @@ -129,6 +135,17 @@ export abstract class IgcButtonBaseComponent extends SizableMixin(
}
}

protected handleMouseDown(event: PointerEvent) {
event.preventDefault();
this.focused = false;
}

protected handleKeyUp() {
if (!this.focused) {
this.focused = true;
}
}

protected formDisabledCallback(state: boolean) {
this._disabled = state;
this.requestUpdate();
Expand All @@ -137,13 +154,17 @@ export abstract class IgcButtonBaseComponent extends SizableMixin(
private renderButton() {
return html`
<button
part="base"
part=${partNameMap({
base: true,
focused: this.focused,
})}
aria-label=${ifDefined(this.ariaLabel ?? nothing)}
?disabled=${this.disabled}
type=${ifDefined(this.type)}
@click=${this.handleClick}
@focus=${this.handleFocus}
@blur=${this.handleBlur}
@pointerdown=${this.handleMouseDown}
>
${this.renderContent()}
</button>
Expand All @@ -153,7 +174,10 @@ export abstract class IgcButtonBaseComponent extends SizableMixin(
private renderLinkButton() {
return html`
<a
part="base"
part=${partNameMap({
base: true,
focused: this.focused,
})}
role="button"
aria-label=${ifDefined(this.ariaLabel ?? nothing)}
aria-disabled=${this.disabled ? 'true' : 'false'}
Expand All @@ -163,6 +187,7 @@ export abstract class IgcButtonBaseComponent extends SizableMixin(
rel=${ifDefined(this.rel)}
@focus=${this.disabled ? nothing : this.handleFocus}
@blur=${this.disabled ? nothing : this.handleBlur}
@pointerdown=${this.disabled ? nothing : this.handleMouseDown}
>
${this.renderContent()}
</a>
Expand Down
6 changes: 3 additions & 3 deletions src/components/button/themes/button/button.base.scss
Expand Up @@ -20,7 +20,7 @@
}
}

[part='base'] {
[part~='base'] {
@include type-style(button);

position: relative;
Expand All @@ -41,8 +41,8 @@
min-width: rem(88px);
}

:host(:not([disabled])) [part='base']:hover,
:host(:not(:disabled)) [part='base']:hover {
:host(:not([disabled])) [part~='base']:hover,
:host(:not(:disabled)) [part~='base']:hover {
cursor: pointer;
}

Expand Down
18 changes: 9 additions & 9 deletions src/components/button/themes/button/shared/button.bootstrap.scss
Expand Up @@ -13,7 +13,7 @@ $outlined-theme: $bootstrap-outlined;
:host([variant='outlined']) {
--component-size: var(--ig-size, #{var-get($outlined-theme, 'default-size')});

[part='base'] {
[part~='base'] {
&:focus {
border: rem(1px) solid var-get($outlined-theme, 'focus-border-color');
}
Expand All @@ -28,8 +28,8 @@ $outlined-theme: $bootstrap-outlined;
--component-size: var(--ig-size, #{var-get($fab-theme, 'default-size')});
}

:host(:not([disabled])[variant='flat']) [part='base'],
:host(:not(:disabled)[variant='flat']) [part='base'] {
:host(:not([disabled])[variant='flat']) [part~='base'],
:host(:not(:disabled)[variant='flat']) [part~='base'] {
&:focus-visible {
box-shadow: 0 0 0 rem(4px) var-get($flat-theme, 'shadow-color');
}
Expand All @@ -41,8 +41,8 @@ $outlined-theme: $bootstrap-outlined;
}
}

:host(:not([disabled])[variant='outlined']) [part='base'],
:host(:not(:disabled)[variant='outlined']) [part='base'] {
:host(:not([disabled])[variant='outlined']) [part~='base'],
:host(:not(:disabled)[variant='outlined']) [part~='base'] {
&:focus-visible {
box-shadow: 0 0 0 rem(4px) var-get($outlined-theme, 'shadow-color');

Expand All @@ -64,8 +64,8 @@ $outlined-theme: $bootstrap-outlined;
}
}

:host(:not([disabled])[variant='contained']) [part='base'],
:host(:not(:disabled)[variant='contained']) [part='base'] {
:host(:not([disabled])[variant='contained']) [part~='base'],
:host(:not(:disabled)[variant='contained']) [part~='base'] {
&:focus-visible {
box-shadow: 0 0 0 rem(4px) var-get($contained-theme, 'shadow-color');
}
Expand All @@ -77,8 +77,8 @@ $outlined-theme: $bootstrap-outlined;
}
}

:host(:not([disabled])[variant='fab']) [part='base'],
:host(:not(:disabled)[variant='fab']) [part='base'] {
:host(:not([disabled])[variant='fab']) [part~='base'],
:host(:not(:disabled)[variant='fab']) [part~='base'] {
&:focus-visible {
box-shadow: 0 0 0 rem(4px) var-get($fab-theme, 'shadow-color');
}
Expand Down
42 changes: 21 additions & 21 deletions src/components/button/themes/button/shared/button.common.scss
Expand Up @@ -7,25 +7,25 @@ $fab-theme: $material-fab;
$outlined-theme: $material-outlined;

/* stylelint-disable */
:host([variant='flat']) [part='base'] {
:host([variant='flat']) [part~='base'] {
border-radius: var-get($flat-theme, 'border-radius');
height: var-get($flat-theme, 'size');
border: rem(1px) solid var-get($flat-theme, 'border-color');
}

:host([variant='outlined']) [part='base'] {
:host([variant='outlined']) [part~='base'] {
border-radius: var-get($outlined-theme, 'border-radius');
height: var-get($outlined-theme, 'size');
border: rem(1px) solid var-get($outlined-theme, 'border-color');
}

:host([variant='contained']) [part='base'] {
:host([variant='contained']) [part~='base'] {
border-radius: var-get($contained-theme, 'border-radius');
height: var-get($contained-theme, 'size');
border: rem(1px) solid var-get($contained-theme, 'border-color');
}

:host([variant='fab']) [part='base'] {
:host([variant='fab']) [part~='base'] {
border: rem(1px) solid var-get($fab-theme, 'border-color');
border-radius: var-get($fab-theme, 'border-radius');
min-height: var-get($fab-theme, 'size');
Expand All @@ -34,7 +34,7 @@ $outlined-theme: $material-outlined;
}
/* stylelint-enable */

[part='base'] {
[part~='base'] {
transition: all .15s ease-in-out;

::slotted(igc-icon) {
Expand All @@ -43,8 +43,8 @@ $outlined-theme: $material-outlined;
}
}

:host(:not([disabled])[variant='flat']) [part='base'],
:host(:not(:disabled)[variant='flat']) [part='base'] {
:host(:not([disabled])[variant='flat']) [part~='base'],
:host(:not(:disabled)[variant='flat']) [part~='base'] {
color: var-get($flat-theme, 'foreground');
background: var-get($flat-theme, 'background');

Expand Down Expand Up @@ -90,8 +90,8 @@ $outlined-theme: $material-outlined;
}
}

:host(:not([disabled])[variant='outlined']) [part='base'],
:host(:not(:disabled)[variant='outlined']) [part='base'] {
:host(:not([disabled])[variant='outlined']) [part~='base'],
:host(:not(:disabled)[variant='outlined']) [part~='base'] {
color: var-get($outlined-theme, 'foreground');
background: var-get($outlined-theme, 'background');

Expand Down Expand Up @@ -138,8 +138,8 @@ $outlined-theme: $material-outlined;
}
}

:host(:not([disabled])[variant='contained']) [part='base'],
:host(:not(:disabled)[variant='contained']) [part='base'] {
:host(:not([disabled])[variant='contained']) [part~='base'],
:host(:not(:disabled)[variant='contained']) [part~='base'] {
color: var-get($contained-theme, 'foreground');
background: var-get($contained-theme, 'background');

Expand Down Expand Up @@ -185,8 +185,8 @@ $outlined-theme: $material-outlined;
}
}

:host(:not([disabled])[variant='fab']) [part='base'],
:host(:not(:disabled)[variant='fab']) [part='base'] {
:host(:not([disabled])[variant='fab']) [part~='base'],
:host(:not(:disabled)[variant='fab']) [part~='base'] {
color: var-get($fab-theme, 'foreground');
background: var-get($fab-theme, 'background');

Expand Down Expand Up @@ -232,8 +232,8 @@ $outlined-theme: $material-outlined;
}
}

:host([disabled][variant='flat']) [part='base'],
:host(:disabled[variant='flat']) [part='base'] {
:host([disabled][variant='flat']) [part~='base'],
:host(:disabled[variant='flat']) [part~='base'] {
color: var-get($flat-theme, 'disabled-foreground');
background: var-get($flat-theme, 'disabled-background');

Expand All @@ -242,8 +242,8 @@ $outlined-theme: $material-outlined;
}
}

:host([disabled][variant='outlined']) [part='base'],
:host(:disabled[variant='outlined']) [part='base'] {
:host([disabled][variant='outlined']) [part~='base'],
:host(:disabled[variant='outlined']) [part~='base'] {
color: var-get($outlined-theme, 'disabled-foreground');
background: var-get($outlined-theme, 'disabled-background');
border: rem(1px) solid var-get($outlined-theme, 'disabled-border-color');
Expand All @@ -253,8 +253,8 @@ $outlined-theme: $material-outlined;
}
}

:host([disabled][variant='contained']) [part='base'],
:host(:disabled[variant='contained']) [part='base'] {
:host([disabled][variant='contained']) [part~='base'],
:host(:disabled[variant='contained']) [part~='base'] {
color: var-get($contained-theme, 'disabled-foreground');
background: var-get($contained-theme, 'disabled-background');

Expand All @@ -263,8 +263,8 @@ $outlined-theme: $material-outlined;
}
}

:host([disabled][variant='fab']) [part='base'],
:host(:disabled[variant='fab']) [part='base'] {
:host([disabled][variant='fab']) [part~='base'],
:host(:disabled[variant='fab']) [part~='base'] {
color: var-get($fab-theme, 'disabled-foreground');
background: var-get($fab-theme, 'disabled-background');

Expand Down
20 changes: 10 additions & 10 deletions src/components/button/themes/button/shared/button.fluent.scss
Expand Up @@ -23,14 +23,14 @@ $outlined-btn-indent: rem(2px);
:host([variant='fab']) {
--component-size: var(--ig-size, #{var-get($fab-theme, 'default-size')});

[part='base'] {
[part~='base'] {
&::after {
border-radius: calc(#{var-get($fab-theme, 'border-radius')} - #{$btn-indent});
}
}
}

[part='base'] {
[part~='base'] {
padding: 0 pad(8px, 12px, 16px);
transition: all .1s ease-in-out;

Expand All @@ -45,17 +45,17 @@ $outlined-btn-indent: rem(2px);
}
}

:host(:not([disabled])[variant='flat']) [part='base'],
:host(:not(:disabled)[variant='flat']) [part='base'] {
:host(:not([disabled])[variant='flat']) [part~='base'],
:host(:not(:disabled)[variant='flat']) [part~='base'] {
&:focus-visible {
&::after {
box-shadow: 0 0 0 rem(1px) var-get($flat-theme, 'focus-visible-border-color');
}
}
}

:host(:not([disabled])[variant='outlined']) [part='base'],
:host(:not(:disabled)[variant='outlined']) [part='base'] {
:host(:not([disabled])[variant='outlined']) [part~='base'],
:host(:not(:disabled)[variant='outlined']) [part~='base'] {
border: rem(1px) solid var-get($outlined-theme, 'border-color');

&:active {
Expand All @@ -73,8 +73,8 @@ $outlined-btn-indent: rem(2px);
}
}

:host(:not([disabled])[variant='contained']) [part='base'],
:host(:not(:disabled)[variant='contained']) [part='base'] {
:host(:not([disabled])[variant='contained']) [part~='base'],
:host(:not(:disabled)[variant='contained']) [part~='base'] {
&:focus-visible {
color: var-get($contained-theme, 'focus-visible-foreground');
background: var-get($contained-theme, 'focus-visible-background');
Expand All @@ -86,8 +86,8 @@ $outlined-btn-indent: rem(2px);
}
}

:host(:not([disabled])[variant='fab']) [part='base'],
:host(:not(:disabled)[variant='fab']) [part='base'] {
:host(:not([disabled])[variant='fab']) [part~='base'],
:host(:not(:disabled)[variant='fab']) [part~='base'] {
&:focus-visible {
color: var-get($fab-theme, 'focus-visible-foreground');
background: var-get($fab-theme, 'focus-visible-background');
Expand Down

0 comments on commit 0730732

Please sign in to comment.