Skip to content

Commit 10bfa1c

Browse files
authored
add icon-only variant for Button (#3460)
1 parent 3aa164c commit 10bfa1c

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

packages/@adobe/spectrum-css-temp/components/button/index.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ governing permissions and limitations under the License.
170170
.spectrum-Button-label + .spectrum-Icon {
171171
margin-inline-end: var(--spectrum-button-primary-text-gap);
172172
}
173+
174+
&.spectrum-Button--iconOnly {
175+
min-inline-size: unset;
176+
padding: var(--spectrum-global-dimension-size-65);
177+
}
173178
}
174179

175180
a.spectrum-Button,

packages/@react-spectrum/button/chromatic/Button.chromatic.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ function render(props: any = {}) {
121121
{...props}>
122122
默認
123123
</Button>
124+
<Button
125+
aria-label="Icon only"
126+
{...props}>
127+
<Bell />
128+
</Button>
124129
</Flex>
125130
);
126131
}

packages/@react-spectrum/button/src/Button.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import {classNames, SlotProvider, useFocusableRef, useSlotProps, useStyleProps} from '@react-spectrum/utils';
13+
import {
14+
classNames,
15+
SlotProvider,
16+
useFocusableRef,
17+
useHasChild,
18+
useSlotProps,
19+
useStyleProps
20+
} from '@react-spectrum/utils';
1421
import {FocusableRef} from '@react-types/shared';
1522
import {FocusRing} from '@react-aria/focus';
1623
import {mergeProps} from '@react-aria/utils';
@@ -43,6 +50,7 @@ function Button<T extends ElementType = 'button'>(props: SpectrumButtonProps<T>,
4350
let {buttonProps, isPressed} = useButton(props, domRef);
4451
let {hoverProps, isHovered} = useHover({isDisabled});
4552
let {styleProps} = useStyleProps(otherProps);
53+
let hasLabel = useHasChild(`.${styles['spectrum-Button-label']}`, domRef);
4654

4755
let buttonVariant = variant;
4856
if (VARIANT_MAPPING[variant]) {
@@ -62,6 +70,7 @@ function Button<T extends ElementType = 'button'>(props: SpectrumButtonProps<T>,
6270
`spectrum-Button--${buttonVariant}`,
6371
{
6472
'spectrum-Button--quiet': isQuiet,
73+
'spectrum-Button--iconOnly': !hasLabel,
6574
'is-disabled': isDisabled,
6675
'is-active': isPressed,
6776
'is-hovered': isHovered

packages/@react-spectrum/button/stories/Button.stories.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ storiesOf('Button', module)
5858
</Flex>
5959
)
6060
)
61+
.add('icon only', () => (
62+
<Button variant="cta">
63+
<Bell />
64+
</Button>
65+
))
6166
.add(
6267
'variant: overBackground',
6368
() => (

0 commit comments

Comments
 (0)