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

Adds theme keypaths to our sx prop type for better autocomplete #1544

Merged
merged 2 commits into from
Oct 28, 2021

Conversation

jfuchs
Copy link
Contributor

@jfuchs jfuchs commented Oct 27, 2021

This replaces the type currently used for the sx prop with one that explicitly includes all key paths into our theme for color and shadow values. This does not yet add better typing to styled system props.

Partially addresses https://github.com/github/primer/issues/389

Open questions

  • Are StyledSystem's ColorProps and ShadowProps objects an acceptable proxy for getting a list of keys into SystemCssProperties objects that have color and shadow variables? In theory styled system prop names could diverge from SystemCssProperties properties' names over time.

Screenshots

CleanShot.2021-10-27.at.15.45.40.mp4

Merge checklist

  • Added/updated tests
  • Added/updated documentation
  • Tested in Chrome
  • Tested in Firefox
  • Tested in Safari
  • Tested in Edge

Take a look at the What we look for in reviews section of the contributing guidelines for more information on how we review PRs.

@changeset-bot
Copy link

changeset-bot bot commented Oct 27, 2021

🦋 Changeset detected

Latest commit: 82a4a68

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 Minor

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

@github-actions
Copy link
Contributor

github-actions bot commented Oct 27, 2021

size-limit report 📦

Path Size
dist/browser.esm.js 54.2 KB (0%)
dist/browser.umd.js 54.6 KB (0%)

Copy link
Contributor

@colebemis colebemis left a comment

Choose a reason for hiding this comment

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

This is an excellent incremental step towards better TypeScript support for the sx prop. No need to over-engineer a solution until we solidify the future of CSS-in-JS in Primer React. Love it 💖

src/theme.ts Outdated
subtle: string
}

// Only meant for Primer components
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if we want autocomplete for primer.* variables. Let's leave them out for now.

src/theme.ts Outdated
export type ThemeShadowPaths = KeyPaths<ThemeShadows>

// Produces a union of dot-delimited keypaths to the string values in a nested object:
type KeyPaths<O extends {}> = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this live in src/utils/types/?

@@ -0,0 +1,5 @@
---
'@primer/components': patch
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm on the fence about whether this is a minor or patch release. Do we consider the lack of types a bug?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, good question. Maybe it's a minor? It's sort of a new feature, but also the API hasn't really changed at all? Even the types should be functionally equivalent — anything that passed typechecking before passes now, and anything that didn't pass before fails now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(TLDR: no strong feelings, happy to switch to minor if you think that's more appropriate)

}
}

export function generatesKeyPathsFromObject(x: KeyPaths<NestedObject>): 'a' | 'b.b1' | 'b.b2' {
Copy link
Contributor

Choose a reason for hiding this comment

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

❤️ type tests

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks again for setting these up!

@jfuchs jfuchs marked this pull request as ready for review October 28, 2021 17:46
@jfuchs jfuchs requested a review from a team October 28, 2021 17:46
@jfuchs jfuchs changed the title better sx typing exploration Adds theme keypaths to our sx prop type for better autocomplete Oct 28, 2021
@jfuchs jfuchs merged commit 5b55b0a into main Oct 28, 2021
@jfuchs jfuchs deleted the jfuchs/handcrafted-theme-type branch October 28, 2021 17:47
@primer-css primer-css mentioned this pull request Oct 28, 2021
@siddharthkp
Copy link
Member

Exciting!!! 🎉

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.

3 participants