1- import { Component , component$ , PropsOf , Slot , useComputed$ } from '@builder.io/qwik'
1+ import { Component , component$ , PropsOf , Signal , Slot , useComputed$ } from '@builder.io/qwik'
22import { ButtonGradient , ButtonMonochromeGradient , ButtonSize , ButtonVariant } from '~/components/Button/button-types'
33import { useButtonClasses } from '~/components/Button/composables/use-button-classes'
44import { useButtonSpinner } from '~/components/Button/composables/use-button-spinner'
@@ -61,6 +61,73 @@ export const Button = component$<ButtonProps>(
6161 full : useComputed$ ( ( ) => full ) ,
6262 } )
6363
64+ const isLinkTag = [ 'a' , Link ] . includes ( tag )
65+ const LinkComponent = isLinkTag ? tag : 'a'
66+ const staticComponent = isLinkTag ? 'button' : tag
67+ const ButtonComponent = href ? LinkComponent : staticComponent
68+
69+ return (
70+ < >
71+ { ButtonComponent === 'button' ? (
72+ < button class = { bindClasses . value } disabled = { ! href ? disabled : undefined } { ...attrs } >
73+ < ButtonInnerComponent
74+ spanClasses = { spanClasses }
75+ color = { color }
76+ gradient = { gradient }
77+ size = { size }
78+ outline = { outline }
79+ loading = { loading }
80+ loadingPosition = { loadingPosition }
81+ prefix = { Prefix }
82+ suffix = { Suffix }
83+ >
84+ < Slot />
85+ </ ButtonInnerComponent >
86+ </ button >
87+ ) : (
88+ < ButtonComponent
89+ class = { bindClasses . value }
90+ href = { href }
91+ target = { href ? attrs . target : undefined }
92+ //@ts -expect-error does not exist on other elements
93+ disabled = { undefined }
94+ { ...attrs }
95+ >
96+ < ButtonInnerComponent
97+ spanClasses = { spanClasses }
98+ color = { color }
99+ gradient = { gradient }
100+ size = { size }
101+ outline = { outline }
102+ loading = { loading }
103+ loadingPosition = { loadingPosition }
104+ prefix = { Prefix }
105+ suffix = { Suffix }
106+ >
107+ < Slot />
108+ </ ButtonInnerComponent >
109+ </ ButtonComponent >
110+ ) }
111+ </ >
112+ )
113+ } ,
114+ )
115+
116+ type ButtonInnerProps = ButtonProps & {
117+ spanClasses : Signal < string >
118+ }
119+ const ButtonInnerComponent = component$ < ButtonInnerProps > (
120+ ( {
121+ spanClasses,
122+ color = 'default' ,
123+ gradient,
124+ size = 'md' ,
125+ outline = false ,
126+ loading = false ,
127+ loadingPosition = 'prefix' ,
128+ prefix : Prefix ,
129+ suffix : Suffix ,
130+ } ) => {
64131 const isOutlineGradient = useComputed$ ( ( ) => outline && gradient )
65132
66133 const loadingPrefix = useComputed$ ( ( ) => loading && loadingPosition === 'prefix' )
@@ -73,20 +140,8 @@ export const Button = component$<ButtonProps>(
73140 outline,
74141 } )
75142
76- const isLinkTag = [ 'a' , Link ] . includes ( tag )
77- const LinkComponent = isLinkTag ? tag : 'a'
78- const staticComponent = isLinkTag ? 'button' : tag
79- const ButtonComponent = href ? LinkComponent : staticComponent
80-
81143 return (
82- < ButtonComponent
83- class = { bindClasses . value }
84- href = { href }
85- target = { href ? attrs . target : undefined }
86- //@ts -expect-error does not exist on other elements
87- disabled = { ! href && ButtonComponent === 'button' ? disabled : undefined }
88- { ...attrs }
89- >
144+ < >
90145 { ! isOutlineGradient . value && ( Prefix || loadingPrefix . value ) && (
91146 < span class = "mr-2" > { loadingPrefix . value ? < Spinner color = { spinnerColor . value } size = { spinnerSize . value } /> : Prefix && < Prefix /> } </ span >
92147 ) }
@@ -106,7 +161,7 @@ export const Button = component$<ButtonProps>(
106161 { ! isOutlineGradient . value && ( Suffix || loadingSuffix . value ) && (
107162 < span class = "ml-2" > { loadingSuffix . value ? < Spinner color = { spinnerColor . value } size = { spinnerSize . value } /> : Suffix && < Suffix /> } </ span >
108163 ) }
109- </ ButtonComponent >
164+ </ >
110165 )
111166 } ,
112167)
0 commit comments