Skip to content

Commit d76de1a

Browse files
authored
chore: Button Style API refactor (#3752)
1 parent 97fff0d commit d76de1a

File tree

2 files changed

+46
-37
lines changed

2 files changed

+46
-37
lines changed

src/button/internal.tsx

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ import {
2222
import Tooltip from '../internal/components/tooltip/index.js';
2323
import { useButtonContext } from '../internal/context/button-context';
2424
import { useSingleTabStopNavigation } from '../internal/context/single-tab-stop-navigation-context';
25-
import { SYSTEM } from '../internal/environment';
2625
import { fireCancelableEvent, isPlainLeftClick } from '../internal/events';
27-
import customCssProps from '../internal/generated/custom-css-properties';
2826
import useForwardFocus from '../internal/hooks/forward-focus';
2927
import { InternalBaseComponentProps } from '../internal/hooks/use-base-component';
3028
import useHiddenDescription from '../internal/hooks/use-hidden-description';
@@ -36,6 +34,7 @@ import InternalLiveRegion from '../live-region/internal';
3634
import { GeneratedAnalyticsMetadataButtonFragment } from './analytics-metadata/interfaces';
3735
import { ButtonIconProps, LeftIcon, RightIcon } from './icon-helper';
3836
import { ButtonProps } from './interfaces';
37+
import { getButtonStyles } from './style';
3938

4039
import analyticsSelectors from './analytics-metadata/styles.css.js';
4140
import styles from './styles.css.js';
@@ -292,41 +291,7 @@ export const InternalButton = React.forwardRef(
292291
</>
293292
);
294293

295-
const stylePropertiesAndVariables =
296-
SYSTEM === 'core'
297-
? {
298-
borderRadius: style?.root?.borderRadius,
299-
borderWidth: style?.root?.borderWidth,
300-
paddingBlock: style?.root?.paddingBlock,
301-
paddingInline: style?.root?.paddingInline,
302-
...(style?.root?.background && {
303-
[customCssProps.styleBackgroundActive]: style.root.background?.active,
304-
[customCssProps.styleBackgroundDefault]: style.root.background?.default,
305-
[customCssProps.styleBackgroundDisabled]: style.root.background?.disabled,
306-
[customCssProps.styleBackgroundHover]: style.root.background?.hover,
307-
}),
308-
...(style?.root?.borderColor && {
309-
[customCssProps.styleBorderColorActive]: style.root.borderColor?.active,
310-
[customCssProps.styleBorderColorDefault]: style.root.borderColor?.default,
311-
[customCssProps.styleBorderColorDisabled]: style.root.borderColor?.disabled,
312-
[customCssProps.styleBorderColorHover]: style.root.borderColor?.hover,
313-
}),
314-
...(style?.root?.color && {
315-
[customCssProps.styleColorActive]: style.root.color?.active,
316-
[customCssProps.styleColorDefault]: style.root.color?.default,
317-
[customCssProps.styleColorDisabled]: style.root.color?.disabled,
318-
[customCssProps.styleColorHover]: style.root.color?.hover,
319-
}),
320-
...(style?.root?.focusRing && {
321-
[customCssProps.styleFocusRingBorderColor]: style.root.focusRing?.borderColor,
322-
[customCssProps.styleFocusRingBorderRadius]: style.root.focusRing?.borderRadius,
323-
[customCssProps.styleFocusRingBorderWidth]: style.root.focusRing?.borderWidth,
324-
}),
325-
...(style?.root?.focusRing?.borderRadius && {
326-
[customCssProps.styleFocusRingBorderRadius]: style.root.focusRing.borderRadius,
327-
}),
328-
}
329-
: undefined;
294+
const stylePropertiesAndVariables = getButtonStyles(style);
330295

331296
if (isAnchor) {
332297
const getAnchorTabIndex = () => {

src/button/style.tsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
import { SYSTEM } from '../internal/environment';
4+
import customCssProps from '../internal/generated/custom-css-properties';
5+
import { ButtonProps } from './interfaces';
6+
7+
export function getButtonStyles(style: ButtonProps['style']) {
8+
if (SYSTEM !== 'core' || !style?.root) {
9+
return undefined;
10+
}
11+
12+
return {
13+
borderRadius: style?.root?.borderRadius,
14+
borderWidth: style?.root?.borderWidth,
15+
paddingBlock: style?.root?.paddingBlock,
16+
paddingInline: style?.root?.paddingInline,
17+
...(style?.root?.background && {
18+
[customCssProps.styleBackgroundActive]: style.root.background?.active,
19+
[customCssProps.styleBackgroundDefault]: style.root.background?.default,
20+
[customCssProps.styleBackgroundDisabled]: style.root.background?.disabled,
21+
[customCssProps.styleBackgroundHover]: style.root.background?.hover,
22+
}),
23+
...(style?.root?.borderColor && {
24+
[customCssProps.styleBorderColorActive]: style.root.borderColor?.active,
25+
[customCssProps.styleBorderColorDefault]: style.root.borderColor?.default,
26+
[customCssProps.styleBorderColorDisabled]: style.root.borderColor?.disabled,
27+
[customCssProps.styleBorderColorHover]: style.root.borderColor?.hover,
28+
}),
29+
...(style?.root?.color && {
30+
[customCssProps.styleColorActive]: style.root.color?.active,
31+
[customCssProps.styleColorDefault]: style.root.color?.default,
32+
[customCssProps.styleColorDisabled]: style.root.color?.disabled,
33+
[customCssProps.styleColorHover]: style.root.color?.hover,
34+
}),
35+
...(style?.root?.focusRing && {
36+
[customCssProps.styleFocusRingBorderColor]: style.root.focusRing?.borderColor,
37+
[customCssProps.styleFocusRingBorderRadius]: style.root.focusRing?.borderRadius,
38+
[customCssProps.styleFocusRingBorderWidth]: style.root.focusRing?.borderWidth,
39+
}),
40+
...(style?.root?.focusRing?.borderRadius && {
41+
[customCssProps.styleFocusRingBorderRadius]: style.root.focusRing.borderRadius,
42+
}),
43+
};
44+
}

0 commit comments

Comments
 (0)