Skip to content
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

feat: wrap spectrum View, Text and Heading to accept custom colors #1903

Merged
merged 6 commits into from
Mar 28, 2024

Conversation

dsmmcken
Copy link
Contributor

@dsmmcken dsmmcken commented Mar 25, 2024

  • Exposes our extra semantic colors to these components
  • Created colorUtils in theme, containing theme variables
  • Move spectrumUtils to spectrum/utils (was originally going to put it here)
  • Added eslint config to allow using UNSAFE_styles/UNSAFE_classname

Tested with:

<Text color="red">css red</Text>
<Text color="red-1000">theme red-1000</Text>
<Text color="accent">accent</Text>
<Text color="positive">positive</Text>
<Text color="negative">negative</Text>
<Text color="info">info</Text>
<Text color="notice">notice</Text>
<Text color="bg">bg</Text>
<Text color="color-mix(in srgb, red, blue)">purple</Text>

- Exposes our extra semantic colors to these components
- Created colorUtils in theme, containing theme variables
- Move spectrumUtils to spectrum/utils (was originally going to put it here)
- Added eslint config to allow using UNSAFE_styles/UNSAFE_classname
@dsmmcken dsmmcken requested a review from bmingles March 25, 2024 21:25
@dsmmcken dsmmcken self-assigned this Mar 25, 2024
@dsmmcken dsmmcken added this to the March 2024 milestone Mar 25, 2024
Copy link

codecov bot commented Mar 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 46.23%. Comparing base (f8a12f7) to head (ad077e0).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1903      +/-   ##
==========================================
+ Coverage   46.20%   46.23%   +0.03%     
==========================================
  Files         641      645       +4     
  Lines       38218    38241      +23     
  Branches     9670     9672       +2     
==========================================
+ Hits        17659    17682      +23     
  Misses      20506    20506              
  Partials       53       53              
Flag Coverage Δ
unit 46.23% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bmingles
Copy link
Contributor

bmingles commented Mar 27, 2024

General idea looks good. I think you may need to memoize the converted props since the UNSAFE_style prop gets re-created on every render. Possible Spectrum handles this gracefully, but IMO it's good to not assume that will be the case.

I think you could incorporate a general util here. Something like:

export function wrapColorProps<
  TProps extends Record<string, unknown> & {
    backgroundColor?: ColorValue;
    color?: ColorValue;
    UNSAFE_style?: React.CSSProperties;
  },
>(props: TProps): Omit<TProps, 'backgroundColor' | 'color'> {
  const { backgroundColor, color, ...rest } = props;

  if (backgroundColor == null && color == null) {
    return props;
  }

  return {
    ...rest,
    UNSAFE_style: {
      ...props.UNSAFE_style,
      backgroundColor:
        backgroundColor == null ? undefined : colorValueStyle(backgroundColor),
      color: color == null ? undefined : colorValueStyle(color),
    },
  };
}

And then consuming it could be like:

const colorProps = useMemo(() => wrapColorProps(props), [props]);
return <SpectrumView {...colorProps} />;

Alternatively, we could make a custom hook like useWrappedColorProps to include the memoization.

Copy link
Contributor

@bmingles bmingles left a comment

Choose a reason for hiding this comment

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

Left some comments. The suggestion for wrapColorProps function may impact the other suggestions, so I would look at that comment first to see what you think.

@dsmmcken dsmmcken requested a review from bmingles March 27, 2024 21:56
@dsmmcken
Copy link
Contributor Author

Kept it simple. Just did a useMemo for style, and allowed colorValueStyle to return undefined. Didn't seem worth a separate abstraction.

bmingles
bmingles previously approved these changes Mar 28, 2024
Copy link
Contributor

@bmingles bmingles left a comment

Choose a reason for hiding this comment

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

LGTM

@dsmmcken
Copy link
Contributor Author

I didn't do an exhaustive search, but replaced some usages. We should add a no-restricted-modules eslint rule later to not allow importing from @adobe/spectrum from outside of the components/spectrum and theme/ folders.

@dsmmcken dsmmcken enabled auto-merge (squash) March 28, 2024 15:16
Copy link
Contributor

@bmingles bmingles left a comment

Choose a reason for hiding this comment

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

LGTM

@dsmmcken dsmmcken merged commit a03fa07 into main Mar 28, 2024
5 checks passed
@dsmmcken dsmmcken deleted the dmckenzie_view/text branch March 28, 2024 16:22
@github-actions github-actions bot locked and limited conversation to collaborators Mar 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants