-
Couldn't load subscription status.
- Fork 2
Use correct CSS version when injecting link tag #187
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
Changes from all commits
2573a54
9ec5813
12ddff0
c7c88e8
12172b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| import '@seamapi/react/index.css' | ||
|
|
||
| import React from 'react' | ||
| import { createRoot } from 'react-dom/client' | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,6 +65,7 @@ | |
| "lint": "eslint --ignore-path .gitignore --ext .js,.cjs,.mjs,.ts,.tsx .", | ||
| "prelint": "prettier --check --ignore-path .gitignore .", | ||
| "postlint": "stylelint '**/*.scss'", | ||
| "prepack": "echo \"export default '$(jq -r .version < package.json)'\" > lib/version.js", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normally I would like to use a platform independent script (can always be done later), but this will ONLY run on CI so it should be safe to do this for now. |
||
| "postversion": "git push --follow-tags", | ||
| "storybook": "storybook dev --port 6006", | ||
| "storybook:docs": "storybook dev --docs --port 6007", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,31 @@ | ||
| import { useEffect } from 'react' | ||
|
|
||
| // TODO figure out the version automatically | ||
| const cssUrl = 'https://www.unpkg.com/@seamapi/react@1.1.0/index.min.css' | ||
| import version from 'lib/version.js' | ||
|
|
||
| const origin = 'https://react.seam.co' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we always use the css file in the release / bundle, wouldn't it always have the correct version? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No because we are injecting a URL that contains the version number which must match the version of the package that is being used by the user. I'm not sure if that explanation is more clear, but it's correct. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yep, was wondering what the benefits of injecting via URL vs. just including what is bundled with the package are? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, there is a pretty long internal discussion about this. The short version is that having it inject the fonts and the styles simplifies adoption and removes all the extra steps you need to use this. It's still possible to opt out of the HTML injection and do it the other way for users who prefer that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was actually thinking of dynamically importing the css via relative paths, but of course that wouldn't work because this is runtime. What if we always include the CSS, but just change the class name? Won't have to deal with version numbers, and no dynamic loading necessary? Root component / providerconst class = disableStyles ? '' : 'seam-components'
return (<div className={class}>...</div>)There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand this. With this update we are always including the css unless the user opts out. I'd prefer not to inject the via an inline style tag since that bloats the js bundle size and cannot take advantage of caching the same way (among other things). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yep, I mean we'd be able to avoid that. Here's a PR: |
||
|
|
||
| export const useSeamStyles = ({ | ||
| disabled = false, | ||
| unminified = false, | ||
| }: { | ||
| unminified?: boolean | ||
| disabled?: boolean | ||
| }) => { | ||
| const ext = `${unminified ? '' : 'min.'}css` | ||
| const cssUrl = `${origin}/v/${version ?? ''}/index.${ext}` | ||
|
|
||
| export const useSeamStyles = ({ enabled }: { enabled: boolean }) => { | ||
| useEffect(() => { | ||
| if (version === null) return | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In development the version will always be null. |
||
| if (disabled) return | ||
| if (globalThis.document == null) return | ||
| if (!enabled) return | ||
| const linkEl = globalThis.document.querySelector(`link[href="${cssUrl}"]`) | ||
|
|
||
| if (linkEl === null) { | ||
| const link = globalThis.document.createElement('link') | ||
| link.rel = 'stylesheet' | ||
| link.type = 'text/css' | ||
| link.href = cssUrl | ||
| const linkEl = globalThis.document.querySelector(`link[href="${cssUrl}"]`) | ||
| if (linkEl != null) return | ||
|
|
||
| globalThis.document.head.appendChild(link) | ||
| } | ||
| }, [enabled]) | ||
| const link = globalThis.document.createElement('link') | ||
| link.rel = 'stylesheet' | ||
| link.type = 'text/css' | ||
| link.href = cssUrl | ||
| globalThis.document.head.appendChild(link) | ||
| }, [disabled, cssUrl]) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export default null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have to put this in the example since when running locally the example should use the latest styles. We can keep the font injection.