Skip to content

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

Merged
jfuchs merged 2 commits into
mainfrom
jfuchs/handcrafted-theme-type
Oct 28, 2021
Merged

Adds theme keypaths to our sx prop type for better autocomplete#1544
jfuchs merged 2 commits into
mainfrom
jfuchs/handcrafted-theme-type

Conversation

@jfuchs

@jfuchsjfuchs commented Oct 27, 2021

Copy link
Copy Markdown
Contributor

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

changeset-botBot commented Oct 27, 2021

Copy link
Copy Markdown

🦋 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
NameType
@primer/componentsMinor

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

github-actionsBot commented Oct 27, 2021

Copy link
Copy Markdown
Contributor

size-limit report 📦

PathSize
dist/browser.esm.js54.2 KB (0%)
dist/browser.umd.js54.6 KB (0%)

@jfuchs
jfuchsforce-pushed the jfuchs/handcrafted-theme-type branch from fd8ec49 to 5de709fCompareOctober 27, 2021 17:33
@jfuchs
jfuchs requested a review from colebemisOctober 27, 2021 22:55

@colebemiscolebemis left a comment

Copy link
Copy Markdown
Contributor

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 💖

Comment threadsrc/theme.ts Outdated
subtle: string
}

// Only meant for Primer components

Copy link
Copy Markdown
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.

Comment threadsrc/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
Copy Markdown
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/?

@jfuchs
jfuchsforce-pushed the jfuchs/handcrafted-theme-type branch from 5de709f to 3095551CompareOctober 28, 2021 17:10
@jfuchs
jfuchsforce-pushed the jfuchs/handcrafted-theme-type branch from 3095551 to bacbdf0CompareOctober 28, 2021 17:16
Comment thread.changeset/silly-coins-sit.md Outdated
@@ -0,0 +1,5 @@
---
'@primer/components': patch

Copy link
Copy Markdown
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
Copy Markdown
ContributorAuthor

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
Copy Markdown
ContributorAuthor

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
Copy Markdown
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
Copy Markdown
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 teamOctober 28, 2021 17:46
@jfuchsjfuchs changed the title better sx typing explorationAdds theme keypaths to our sx prop type for better autocompleteOct 28, 2021
@jfuchs
jfuchs merged commit 5b55b0a into mainOct 28, 2021
@jfuchs
jfuchs deleted the jfuchs/handcrafted-theme-type branch October 28, 2021 17:47
@primer-cssprimer-css mentioned this pull request Oct 28, 2021
@siddharthkp

Copy link
Copy Markdown
Member

Exciting!!! 🎉

@primer-cssprimer-css mentioned this pull request Nov 16, 2021
Sign up for freeto 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

@jfuchs@siddharthkp@colebemis