Last active
July 5, 2024 04:09
-
-
Save joshdavenport/3e6d7fed02fba0cf47b6f85d4e2faf40 to your computer and use it in GitHub Desktop.
Revisions
-
joshdavenport revised this gist
Feb 22, 2024 . 1 changed file with 14 additions and 1 deletion.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 charactersOriginal file line number Diff line number Diff line change @@ -1,10 +1,23 @@ /* The main method here is processTailwindSrcsetSizes. You can generate a sizes attribute using it based on tailwind style breakpointed sizes referencing your screens, such as: "100vw md:500px" "200px lg:100vw" "200px md:300px xl:600px" "100vw lg:50vw" "100vw" "400px" Your full tailwind config is resolved and screens from it used, so you can use whatever you normally use. Given the default tailwind screens the above becomes "(min-width: 768px) 500px, 100vw" "(min-width: 1024px), 200px" "(min-width: 1280px) 600px, (min-width: 768px) 300px, 200px" "(min-width: 1024px) 50vw, 100vw" "100vw" "400px" I use it in a React component by taking a sizes prop on the component, then passing that value into processTailwindSrcsetSizes then using that as the actual sizes attribute. -
joshdavenport revised this gist
Feb 22, 2024 . 1 changed file with 5 additions and 2 deletions.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 charactersOriginal file line number Diff line number Diff line change @@ -60,7 +60,10 @@ export function processTailwindSrcsetSizes(sizes: string) { } return convertedSizes .sort((a, b) => b.size - a.size) .map(({ size, width }) => size === 0 ? width : `(min-width: ${size}px) ${width}`, ) .join(', '); } -
joshdavenport revised this gist
Feb 22, 2024 . 1 changed file with 5 additions and 4 deletions.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 charactersOriginal file line number Diff line number Diff line change @@ -5,6 +5,9 @@ tailwind style breakpointed sizes such as: "100vw md:500px" "200px md:300px xl:600px" "100vw" I use it in a React component by taking a sizes prop on the component, then passing that value into processTailwindSrcsetSizes then using that as the actual sizes attribute. */ import resolveConfig from 'tailwindcss/resolveConfig'; @@ -56,10 +59,8 @@ export function processTailwindSrcsetSizes(sizes: string) { return null; } return convertedSizes .sort((a, b) => a.size - b.size) .map(({ size, width }) => (size === 0 ? width : `${size}w ${width}`)) .join(', '); } -
joshdavenport created this gist
Feb 22, 2024 .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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,65 @@ /* The main method here is processTailwindSrcsetSizes. You can generate a sizes attribute using it based on tailwind style breakpointed sizes such as: "100vw md:500px" "200px md:300px xl:600px" "100vw" */ import resolveConfig from 'tailwindcss/resolveConfig'; // Resolve correctly based on your codebase import tailwindConfig from '@/../tailwind.config.js'; const fullConfig = resolveConfig(tailwindConfig); export function isTailwindScreen( screen: string, ): screen is keyof typeof fullConfig.theme.screens { return screen in fullConfig.theme.screens; } export function processTailwindSrcsetSizes(sizes: string) { type TailwindSizesSize = { size: number; width: string; }; const convertedSizes = sizes .split(' ') .map((rule) => { const [screenOrSoleWidth, width] = rule.split(':'); if (screenOrSoleWidth && !width) { return { size: 0, width: screenOrSoleWidth, }; } if ( !screenOrSoleWidth || !width || !isTailwindScreen(screenOrSoleWidth) ) { return null; } return { size: parseInt(fullConfig.theme.screens[screenOrSoleWidth]), width: width, }; }) .filter((rule) => rule !== null) as TailwindSizesSize[]; if (!convertedSizes.length) { return null; } const sizesAttributified = convertedSizes .sort((a, b) => a.size - b.size) .map(({ size, width }) => (size === 0 ? width : `${size}w ${width}`)) .join(', '); return sizesAttributified; }