Skip to content

chore: Button Style API refactor #3752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 2 additions & 37 deletions src/button/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ import {
import Tooltip from '../internal/components/tooltip/index.js';
import { useButtonContext } from '../internal/context/button-context';
import { useSingleTabStopNavigation } from '../internal/context/single-tab-stop-navigation-context';
import { SYSTEM } from '../internal/environment';
import { fireCancelableEvent, isPlainLeftClick } from '../internal/events';
import customCssProps from '../internal/generated/custom-css-properties';
import useForwardFocus from '../internal/hooks/forward-focus';
import { InternalBaseComponentProps } from '../internal/hooks/use-base-component';
import useHiddenDescription from '../internal/hooks/use-hidden-description';
Expand All @@ -36,6 +34,7 @@ import InternalLiveRegion from '../live-region/internal';
import { GeneratedAnalyticsMetadataButtonFragment } from './analytics-metadata/interfaces';
import { ButtonIconProps, LeftIcon, RightIcon } from './icon-helper';
import { ButtonProps } from './interfaces';
import { getButtonStyles } from './style';

import analyticsSelectors from './analytics-metadata/styles.css.js';
import styles from './styles.css.js';
Expand Down Expand Up @@ -292,41 +291,7 @@ export const InternalButton = React.forwardRef(
</>
);

const stylePropertiesAndVariables =
SYSTEM === 'core'
? {
borderRadius: style?.root?.borderRadius,
borderWidth: style?.root?.borderWidth,
paddingBlock: style?.root?.paddingBlock,
paddingInline: style?.root?.paddingInline,
...(style?.root?.background && {
[customCssProps.styleBackgroundActive]: style.root.background?.active,
[customCssProps.styleBackgroundDefault]: style.root.background?.default,
[customCssProps.styleBackgroundDisabled]: style.root.background?.disabled,
[customCssProps.styleBackgroundHover]: style.root.background?.hover,
}),
...(style?.root?.borderColor && {
[customCssProps.styleBorderColorActive]: style.root.borderColor?.active,
[customCssProps.styleBorderColorDefault]: style.root.borderColor?.default,
[customCssProps.styleBorderColorDisabled]: style.root.borderColor?.disabled,
[customCssProps.styleBorderColorHover]: style.root.borderColor?.hover,
}),
...(style?.root?.color && {
[customCssProps.styleColorActive]: style.root.color?.active,
[customCssProps.styleColorDefault]: style.root.color?.default,
[customCssProps.styleColorDisabled]: style.root.color?.disabled,
[customCssProps.styleColorHover]: style.root.color?.hover,
}),
...(style?.root?.focusRing && {
[customCssProps.styleFocusRingBorderColor]: style.root.focusRing?.borderColor,
[customCssProps.styleFocusRingBorderRadius]: style.root.focusRing?.borderRadius,
[customCssProps.styleFocusRingBorderWidth]: style.root.focusRing?.borderWidth,
}),
...(style?.root?.focusRing?.borderRadius && {
[customCssProps.styleFocusRingBorderRadius]: style.root.focusRing.borderRadius,
}),
}
: undefined;
const stylePropertiesAndVariables = getButtonStyles(style);

if (isAnchor) {
const getAnchorTabIndex = () => {
Expand Down
44 changes: 44 additions & 0 deletions src/button/style.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { SYSTEM } from '../internal/environment';
import customCssProps from '../internal/generated/custom-css-properties';
import { ButtonProps } from './interfaces';

export function getButtonStyles(style: ButtonProps['style']) {
if (SYSTEM !== 'core' || !style?.root) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: just out of curiosity, I know we have this for bundlers/minifiers to remove, but I don't know how complex this is allowed to be. For example, are you allowed to combine it with other conditionals like this?

Do you know if there's a way to test this? Got curious, did some research:

Oh well, probably need to come up with something smarter eventually. But that's for later, this isn't a valid reason to block this PR.

return undefined;
}

return {
borderRadius: style?.root?.borderRadius,
borderWidth: style?.root?.borderWidth,
paddingBlock: style?.root?.paddingBlock,
paddingInline: style?.root?.paddingInline,
...(style?.root?.background && {
[customCssProps.styleBackgroundActive]: style.root.background?.active,
[customCssProps.styleBackgroundDefault]: style.root.background?.default,
[customCssProps.styleBackgroundDisabled]: style.root.background?.disabled,
[customCssProps.styleBackgroundHover]: style.root.background?.hover,
}),
...(style?.root?.borderColor && {
[customCssProps.styleBorderColorActive]: style.root.borderColor?.active,
[customCssProps.styleBorderColorDefault]: style.root.borderColor?.default,
[customCssProps.styleBorderColorDisabled]: style.root.borderColor?.disabled,
[customCssProps.styleBorderColorHover]: style.root.borderColor?.hover,
}),
...(style?.root?.color && {
[customCssProps.styleColorActive]: style.root.color?.active,
[customCssProps.styleColorDefault]: style.root.color?.default,
[customCssProps.styleColorDisabled]: style.root.color?.disabled,
[customCssProps.styleColorHover]: style.root.color?.hover,
}),
...(style?.root?.focusRing && {
[customCssProps.styleFocusRingBorderColor]: style.root.focusRing?.borderColor,
[customCssProps.styleFocusRingBorderRadius]: style.root.focusRing?.borderRadius,
[customCssProps.styleFocusRingBorderWidth]: style.root.focusRing?.borderWidth,
}),
...(style?.root?.focusRing?.borderRadius && {
[customCssProps.styleFocusRingBorderRadius]: style.root.focusRing.borderRadius,
}),
};
}
Loading