11---
22import type { ButtonLabelOptions , ButtonProps } from ' @components/Button/server'
33import { buildButtonClassList , resolveAriaLabel } from ' @components/Button/server'
4- import Icon from ' @components/Icon/index.astro'
4+ import AnchorLayout from ' @components/Button/layouts/anchor.astro'
5+ import DefaultLayout from ' @components/Button/layouts/default.astro'
56
67type Props = ButtonProps
78
@@ -27,6 +28,9 @@ const buttonClassList = buildButtonClassList({
2728 variant ,
2829 size ,
2930 additionalClasses ,
31+ icon ,
32+ iconPosition ,
33+ text ,
3034})
3135
3236// Determine icon size based on button size if not specified
@@ -49,51 +53,50 @@ if (typeof icon !== 'undefined') {
4953const computedAriaLabel = resolveAriaLabel (labelOptions )
5054const ariaAttributes = computedAriaLabel ? { ' aria-label' : computedAriaLabel } : {}
5155
52- const linkHref = disabled ? undefined : href
53- const linkOnClick = disabled ? undefined : onClick
56+ const hasAriaAttributes = Object .keys (ariaAttributes ).length > 0
57+ const anchorOptionalProps = {
58+ ... (id !== undefined ? { id } : {}),
59+ ... (onClick !== undefined ? { onClick } : {}),
60+ ... (icon !== undefined ? { icon } : {}),
61+ ... (text !== undefined ? { text } : {}),
62+ ... (hasAriaAttributes ? { ariaAttributes } : {}),
63+ }
64+
65+ const defaultOptionalProps = {
66+ ... (id !== undefined ? { id } : {}),
67+ ... (onClick !== undefined ? { onClick } : {}),
68+ ... (icon !== undefined ? { icon } : {}),
69+ ... (text !== undefined ? { text } : {}),
70+ ... (hasAriaAttributes ? { ariaAttributes } : {}),
71+ }
5472---
5573
5674<div style =" display: contents" >
5775 {
5876 href ? (
59- <a
60- href = { linkHref }
61- class :list = { buttonClassList }
62- id = { id }
63- onclick = { linkOnClick }
64- aria-disabled = { disabled ? ' true' : undefined }
65- tabindex = { disabled ? - 1 : undefined }
66- { ... ariaAttributes }
67- { ... rest }
77+ <AnchorLayout
78+ href = { href }
79+ disabled = { disabled }
80+ classList = { buttonClassList }
81+ iconPosition = { iconPosition }
82+ iconSize = { defaultIconSize }
83+ attributes = { rest }
84+ { ... anchorOptionalProps }
6885 >
69- { icon && (iconPosition === ' left' || iconPosition === ' only' ) && (
70- <Icon icon = { icon } size = { defaultIconSize } classes = { text ? ' mr-2' : ' ' } />
71- )}
72- { text && <span >{ text } </span >}
73- { icon && iconPosition === ' right' && (
74- <Icon icon = { icon } size = { defaultIconSize } classes = " ml-2" />
75- )}
7686 <slot />
77- </a >
87+ </AnchorLayout >
7888 ) : (
79- <button
89+ <DefaultLayout
8090 type = { type }
81- class :list = { buttonClassList }
8291 disabled = { disabled }
83- id = { id }
84- onclick = { onClick }
85- { ... ariaAttributes }
86- { ... rest }
92+ classList = { buttonClassList }
93+ iconPosition = { iconPosition }
94+ iconSize = { defaultIconSize }
95+ attributes = { rest }
96+ { ... defaultOptionalProps }
8797 >
88- { icon && (iconPosition === ' left' || iconPosition === ' only' ) && (
89- <Icon icon = { icon } size = { defaultIconSize } classes = { text ? ' mr-2' : ' ' } />
90- )}
91- { text && <span >{ text } </span >}
92- { icon && iconPosition === ' right' && (
93- <Icon icon = { icon } size = { defaultIconSize } classes = " ml-2" />
94- )}
9598 <slot />
96- </button >
99+ </DefaultLayout >
97100 )
98101 }
99102</div >
0 commit comments