1- import styled , { css } from 'styled-components' ;
1+ import clsx from 'clsx' ;
2+ import { CSSProperties } from 'react' ;
23
34import { Colors } from './Color' ;
5+ import styles from './css/ButtonLink.module.css' ;
46
57type Colors =
68 | string
@@ -12,67 +14,46 @@ type Colors =
1214
1315type Underline = 'never' | 'always' | 'hover' ;
1416
15- const fontColor = ( color : Colors ) => {
16- if ( typeof color === 'string' ) {
17- return css `
18- color : ${ color } ;
19- ` ;
20- }
21-
22- const { link, hover, active} = color ;
23- return css `
24- color : ${ link } ;
25- ${ hover ? `&:hover { color: ${ hover } ; }` : null }
26- ${ active ? `&:active { color: ${ active } ; }` : null }
27- ` ;
17+ const getColorVars = ( color : Colors ) => {
18+ const values = typeof color === 'string' ? { link : color , hover : color , active : color } : color ;
19+ return {
20+ '--button-link-color' : values . link ,
21+ '--button-link-hover-color' : values . hover ,
22+ '--button-link-active-color' : values . active ,
23+ } as CSSProperties ;
2824} ;
2925
30- const textDecoration = ( underline : Underline ) => {
31- switch ( underline ) {
32- case 'always' :
33- return css `
34- text-decoration : underline;
35- ` ;
36- case 'hover' :
37- return css `
38- & : hover : not (: disabled ) {
39- text-decoration : underline;
40- }
41- ` ;
42- case 'never' :
43- default :
44- return null ;
45- }
26+ const getTextDecorationClassName = ( underline : Underline ) => {
27+ return clsx (
28+ underline === 'always' && styles . textDecorationAlways ,
29+ underline === 'hover' && styles . textDecorationHover ,
30+ ) ;
4631} ;
4732
4833interface Props extends Omit < React . ButtonHTMLAttributes < HTMLButtonElement > , 'color' > {
4934 color ?: Colors ;
5035 underline ?: Underline ;
5136}
5237
53- export const ButtonLink = styled ( ( { color : _color , underline : _underline , ...rest } : Props ) => (
54- < button { ...rest } />
55- ) ) `
56- background: transparent;
57- border: 0;
58- cursor: pointer;
59- font-size: inherit;
60- line-height: 1;
61- padding: 8px;
62- margin: -8px;
63- text-align: left;
64-
65- &:disabled {
66- cursor: default;
67- opacity: 0.7;
68- }
69-
70- &:focus:not(:focus-visible) {
71- outline: none;
72- }
73-
74- transition: 30ms color linear;
75-
76- ${ ( { color} ) => fontColor ( color || Colors . linkDefault ( ) ) }
77- ${ ( { underline} ) => textDecoration ( underline || 'hover' ) }
78- ` ;
38+ export const ButtonLink = ( {
39+ color,
40+ underline,
41+ style : userStyle ,
42+ className : userClassName ,
43+ ...rest
44+ } : Props ) => {
45+ const colors = color ? getColorVars ( color ) : { } ;
46+ const textDecoration = underline ? getTextDecorationClassName ( underline ) : undefined ;
47+ const style = {
48+ ...userStyle ,
49+ ...colors ,
50+ } ;
51+
52+ return (
53+ < button
54+ { ...rest }
55+ style = { style }
56+ className = { clsx ( styles . buttonLink , textDecoration , userClassName ) }
57+ />
58+ ) ;
59+ } ;
0 commit comments