Skip to content

Commit 5f0e6c5

Browse files
committed
feat(components): button component
1 parent a56a1dd commit 5f0e6c5

File tree

1 file changed

+12
-33
lines changed

1 file changed

+12
-33
lines changed

packages/components/src/button/button.ts

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {html, noChange, nothing} from 'lit/html.js';
88
import {literal, html as staticHtml} from 'lit/static-html.js';
99

1010
import {icon} from '../icon/icon.js';
11+
import {gecutEFO, type EventsObject} from '../internal/events-handler.js';
1112

1213
import type {IconContent} from '../icon/icon.js';
1314
import type {ClassInfo} from 'lit/directives/class-map.js';
@@ -28,8 +29,7 @@ export interface ButtonContent {
2829
href?: string;
2930
target?: '_blank' | '_parent' | '_self' | '_top';
3031

31-
onClick?: (event: MouseEvent) => void;
32-
onDblClick?: (event: MouseEvent) => void;
32+
events?: EventsObject;
3333

3434
label?: string;
3535
}
@@ -42,13 +42,6 @@ export class GecutButtonDirective extends GecutDirective {
4242
protected content?: ButtonContent;
4343
protected type: 'link' | 'button' = 'button';
4444

45-
protected $rootClassName =
46-
'relative group rounded-full h-10 px-6 cursor-pointer focus-ring disabled:cursor-default disabled:pointer-events-none';
47-
protected $loaderClassName =
48-
'absolute inset-0 flex justify-center items-center transition-opacity duration-300 opacity-0 group-[[loading]]:opacity-100 [&[loading]]:cursor-default [&[loading]]:pointer-events-none';
49-
protected $bodyClassName =
50-
'flex items-center justify-center h-full w-full gap-2 transition-opacity duration-300 opacity-100 group-[[loading]]:opacity-0';
51-
5245
render(content?: ButtonContent): unknown {
5346
this.log.methodArgs?.('render', content);
5447

@@ -79,16 +72,15 @@ export class GecutButtonDirective extends GecutDirective {
7972

8073
return staticHtml`
8174
<${tag}
82-
class=${classMap({[this.$rootClassName]: true, ...this.getRenderClasses()})}
75+
class=${classMap(this.getRenderClasses())}
8376
role="button"
8477
href=${ifDefined(this.content.href)}
8578
target=${ifDefined(this.content.target)}
8679
tabindex="${this.content.disabled ? -1 : 0}"
8780
?disabled=${this.content.disabled ?? false}
8881
?loading=${this.content.loading ?? false}
89-
@click=${this.content.onClick}
90-
@dblclick=${this.content.onDblClick}
91-
>${this.renderLoader()}${this.renderBody()}</${tag}>
82+
${gecutEFO(this.content.events)}
83+
>${this.renderLoader()}${this.renderBody()}</${tag}>
9284
`;
9385
}
9486
protected renderBody(): unknown {
@@ -97,15 +89,7 @@ export class GecutButtonDirective extends GecutDirective {
9789
this.log.method?.('renderContent');
9890

9991
return html`
100-
<div class=${this.$loaderClassName}>
101-
${icon(
102-
this.content.loader ?? {
103-
svg: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><g stroke="currentColor"><circle cx="12" cy="12" r="9.5" fill="none" stroke-linecap="round" stroke-width="2.5"><animate attributeName="stroke-dasharray" calcMode="spline" dur="1.5s" keySplines="0.42,0,0.58,1;0.42,0,0.58,1;0.42,0,0.58,1" keyTimes="0;0.475;0.95;1" repeatCount="indefinite" values="0 150;42 150;42 150;42 150"/><animate attributeName="stroke-dashoffset" calcMode="spline" dur="1.5s" keySplines="0.42,0,0.58,1;0.42,0,0.58,1;0.42,0,0.58,1" keyTimes="0;0.475;0.95;1" repeatCount="indefinite" values="0;-16;-59;-59"/></circle><animateTransform attributeName="transform" dur="2s" repeatCount="indefinite" type="rotate" values="0 12 12;360 12 12"/></g></svg>',
104-
},
105-
)}
106-
</div>
107-
108-
<div class=${this.$bodyClassName}>
92+
<div class="gecut-button-body">
10993
${when(this.content.icon?.svg, () => icon({svg: this.content?.icon?.svg as string}))}
11094
11195
<span class="text-labelLarge">${this.content.label}</span>
@@ -120,7 +104,7 @@ export class GecutButtonDirective extends GecutDirective {
120104
this.log.method?.('renderLoader');
121105

122106
return html`
123-
<div class=${this.$loaderClassName}>
107+
<div class="gecut-button-loader">
124108
${icon(
125109
this.content.loader ?? {
126110
svg: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><g stroke="currentColor"><circle cx="12" cy="12" r="9.5" fill="none" stroke-linecap="round" stroke-width="2.5"><animate attributeName="stroke-dasharray" calcMode="spline" dur="1.5s" keySplines="0.42,0,0.58,1;0.42,0,0.58,1;0.42,0,0.58,1" keyTimes="0;0.475;0.95;1" repeatCount="indefinite" values="0 150;42 150;42 150;42 150"/><animate attributeName="stroke-dashoffset" calcMode="spline" dur="1.5s" keySplines="0.42,0,0.58,1;0.42,0,0.58,1;0.42,0,0.58,1" keyTimes="0;0.475;0.95;1" repeatCount="indefinite" values="0;-16;-59;-59"/></circle><animateTransform attributeName="transform" dur="2s" repeatCount="indefinite" type="rotate" values="0 12 12;360 12 12"/></g></svg>',
@@ -134,16 +118,11 @@ export class GecutButtonDirective extends GecutDirective {
134118
return {
135119
...super.getRenderClasses(),
136120

137-
'text-primary bg-surfaceContainerLow elevation-1 hover:elevation-2 hover:stateHover-primary focus:elevation-1 active:stateActive-primary disabled:opacity-60':
138-
this.content?.type === 'elevated',
139-
'text-onPrimary bg-primary elevation-0 hover:elevation-2 hover:stateHover-onPrimary focus:elevation-1 active:stateActive-onPrimary disabled:opacity-40':
140-
this.content?.type === 'filled',
141-
'text-onSecondaryContainer bg-secondaryContainer elevation-0 hover:elevation-2 hover:stateHover-onSecondaryContainer focus:elevation-1 active:stateActive-onSecondaryContainer disabled:opacity-60':
142-
this.content?.type === 'filledTonal',
143-
'text-primary bg-transparent border border-outline hover:stateHover-primary active:stateActive-primary disabled:opacity-60':
144-
this.content?.type === 'outlined',
145-
'text-primary bg-transparent hover:stateHover-primary active:stateActive-primary disabled:opacity-60':
146-
this.content?.type === 'text',
121+
elevated: this.content?.type === 'elevated',
122+
filled: this.content?.type === 'filled',
123+
'filled-tonal': this.content?.type === 'filledTonal',
124+
outlined: this.content?.type === 'outlined',
125+
text: this.content?.type === 'text',
147126
};
148127
}
149128
}

0 commit comments

Comments
 (0)