-
Notifications
You must be signed in to change notification settings - Fork 779
chore(tailwind): Small refactor for easier changes and ease of understanding #1351
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5f5b660
chore(tailwind): Simplify mechanism for processing Tailwind children
gabrielmfern 3aed882
chore(tailwind): Organize a bit more
gabrielmfern 150f587
chore(tailwind): Organize imports and rename getStylesPerClasSMap
gabrielmfern 777f897
chore(tailwind): Improve a comment on the processElement
gabrielmfern 90644f3
feat(tailwind): More organized appraoch on element processing and sty…
gabrielmfern d49cd7f
chore(tailwind): Format
gabrielmfern 06ed58e
chore(tailwind): Remove sneaky .only
gabrielmfern 62bdbdb
fix(tailwind): Wrong expected sanitized media queries from the useTai…
gabrielmfern 78bcc36
chore(tailwind): Format
gabrielmfern File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Creates a style inlining function that converts an element's className into inlined React styles - based on the | ||
* {@link stylePerClass} map - for all classes also. | ||
* | ||
* Also returns residual classes that could not be found on the map. | ||
*/ | ||
export function useStyleInlining( | ||
stylePerClass: Record<string, React.CSSProperties> | ||
) { | ||
return (className: string) => { | ||
const classes = className.split(' '); | ||
|
||
const residualClasses = []; | ||
let styles: React.CSSProperties = {}; | ||
|
||
for (const singleClass of classes) { | ||
if (singleClass in stylePerClass) { | ||
styles = { | ||
...styles, | ||
...stylePerClass[singleClass] | ||
}; | ||
} else { | ||
residualClasses.push(singleClass); | ||
} | ||
} | ||
|
||
return { | ||
styles, | ||
residualClassName: residualClasses.join(' '), | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { useTailwindStyles } from "./use-tailwind-styles"; | ||
|
||
describe("useTailwindStyles()", () => { | ||
test("with basic media queries and nested elements", () => { | ||
const node = ( | ||
<div className="w-full md:w-[250px] bg-red-500"> | ||
<span className="well-hello text-red-100">Well, hello friends!</span> | ||
</div> | ||
); | ||
|
||
expect(useTailwindStyles(node, {})).toEqual({ | ||
stylePerClassMap: { | ||
"w-full": { width: "100%" }, | ||
"bg-red-500": { backgroundColor: "rgb(239,68,68)" }, | ||
"text-red-100": { color: "rgb(254,226,226)" }, | ||
}, | ||
sanitizedMediaQueries: [".md_w-250px {width: 250px!important}\n"], | ||
nonInlinableClasses: ["md:w-[250px]"], | ||
}); | ||
}); | ||
|
||
test.only("with more media queries", () => { | ||
const node = ( | ||
<div className="bg-red-200 sm:bg-red-300 md:bg-red-400 lg:bg-red-500" /> | ||
); | ||
|
||
expect(useTailwindStyles(node, {})).toEqual({ | ||
stylePerClassMap: { | ||
"bg-red-200": { backgroundColor: "rgb(254,202,202)" }, | ||
}, | ||
sanitizedMediaQueries: [ | ||
"@media (min-width: 640px) {.sm_bg-red-300 {background-color: rgb(252,165,165)!important}}", | ||
"@media (min-width: 768px) {.md_bg-red-400 {background-color: rgb(248,113,113)!important}}", | ||
"@media (min-width: 1024px) {.lg_bg-red-500 {background-color: rgb(239,68,68)!important}}", | ||
], | ||
nonInlinableClasses: ["sm:bg-red-300", "md:bg-red-400", "lg:bg-red-500"], | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React from "react"; | ||
import type { TailwindConfig } from "../tailwind"; | ||
import { separateMediaQueriesFromCSS } from "../utils/css/media-queries/separate-media-queries-from-css"; | ||
import { rulesFor } from "../utils/css/rules-for"; | ||
import { quickSafeRenderToString } from "../utils/quick-safe-render-to-string"; | ||
import { getCssForMarkup } from "../utils/tailwindcss/get-css-for-markup"; | ||
import { useRgbNonSpacedSyntax } from "../utils/compatibility/use-rgb-non-spaced-syntax"; | ||
import { cssToJsxStyle } from "../utils/compatibility/css-to-jsx-style"; | ||
import { unescapeClass } from "../utils/compatibility/unescape-class"; | ||
import { sanitizeRuleSelector } from "../utils/compatibility/sanitize-rule-selector"; | ||
import { makeAllRulePropertiesImportant } from "../utils/compatibility/make-all-rule-properties-important"; | ||
|
||
/** | ||
* Gets all the necessary information from the node and the Tailwind config to be able | ||
* to apply all the Tailwind styles. | ||
*/ | ||
export function useTailwindStyles( | ||
gabrielmfern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
node: React.ReactNode, | ||
config: TailwindConfig, | ||
) { | ||
const markup = quickSafeRenderToString(<>{node}</>); | ||
const css = useRgbNonSpacedSyntax(getCssForMarkup(markup, config)); | ||
|
||
const [cssWithoutMediaQueries, mediaQueries] = | ||
separateMediaQueriesFromCSS(css); | ||
|
||
const stylePerClassMap: Record<string, React.CSSProperties> = {}; | ||
for (const rule of rulesFor(cssWithoutMediaQueries)) { | ||
const unescapedClass = unescapeClass(rule.selector); | ||
stylePerClassMap[unescapedClass] = cssToJsxStyle(rule.content); | ||
} | ||
|
||
const nonInlinableClasses: string[] = []; | ||
|
||
const sanitizedMediaQueries = mediaQueries.map((mediaQuery) => { | ||
let sanitizedMediaQuery = mediaQuery; | ||
for (const rule of rulesFor(mediaQuery)) { | ||
nonInlinableClasses.push(unescapeClass(rule.selector)); | ||
|
||
sanitizedMediaQuery = sanitizedMediaQuery.replace( | ||
rule.value, | ||
rule.value | ||
.replace(rule.selector, sanitizeRuleSelector(rule.selector)) | ||
.replace(rule.content, makeAllRulePropertiesImportant(rule.content)) | ||
.trim(), | ||
); | ||
} | ||
return sanitizedMediaQuery | ||
.replace(/(\r\n|\r|\n)+/gm, "") | ||
.replace(/\s+/gm, " "); | ||
}); | ||
|
||
return { | ||
stylePerClassMap, | ||
sanitizedMediaQueries, | ||
nonInlinableClasses, | ||
}; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.