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

Handle invalid color scheme names #1286

Merged
merged 3 commits into from
Jun 10, 2021
Merged

Conversation

colebemis
Copy link
Contributor

Problem

If ThemeProvider is a passed an invalid color scheme name (e.g. <ThemeProvider dayScheme="foo">), it could potentially break an entire application. We noticed this happening in an internal React application.

Solution

This PR updates ThemeProvider to default to the first defined color scheme if it's passed an invalid color scheme name.

@changeset-bot
Copy link

changeset-bot bot commented Jun 10, 2021

🦋 Changeset detected

Latest commit: f0f8edc

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/components Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Jun 10, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/primer/primer-components/HWjrVkvbSqeC5smKVHTpXSZam3xH
✅ Preview: https://primer-components-git-handle-unrecognized-color-schemes-primer.vercel.app

@vercel vercel bot temporarily deployed to Preview June 10, 2021 16:32 Inactive
@colebemis colebemis requested a review from smockle June 10, 2021 16:33
return theme

// Apply the first defined color scheme
const defaultColorScheme = Object.keys(theme.colorSchemes)[0]
Copy link
Member

@smockle smockle Jun 10, 2021

Choose a reason for hiding this comment

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

Non-blocking: I wonder if index 0 is magic-numbery? Though it may be our best option, since the Theme type definition doesn’t require a default/fallback scheme to be designated (example below). 🤔

export type Theme = {
  colorSchemes: {
    default: { [key: string]: any }
    [key: string]: any
  }
  [key: string]: any
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that [0] feels a little bit wrong here. Unfortunetly, we can't make any assumptions about the format of the theme object without making a breaking change to the library. Let's move forward with this "incrementally correct" solution, and we can revisit TypeScript types for the theme object later.

Copy link
Member

@dgreif dgreif left a comment

Choose a reason for hiding this comment

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

Great change. Definitely makes sense to have a fallback here

@colebemis colebemis merged commit 2793ef4 into main Jun 10, 2021
@colebemis colebemis deleted the handle-unrecognized-color-schemes branch June 10, 2021 17:18
@github-actions github-actions bot mentioned this pull request Jun 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants