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

[Feature]: generate px instead of rem for tailwind classes #437

Open
GorvGoyl opened this issue Jan 13, 2023 · 6 comments
Open

[Feature]: generate px instead of rem for tailwind classes #437

GorvGoyl opened this issue Jan 13, 2023 · 6 comments
Labels
🛑 Abandoned The issue or Pull Request will not be worked on ✨ Feature Request Something should be added

Comments

@GorvGoyl
Copy link

Describe the problem

I'm using twind.style to inject tailwind styles into shadow dom for a chrome extension that runs on different sites. The issue is rem is calculated based on the base font size set of html element and this varies from site to site. Because of this the default font-size is different on different sites. Is there a way to get CSS output as px instead of rem units? This should make the shadow dom UI consistent across sites

Describe the proposed solution

ability to get px instead of rem for tailwind classes.

Alternatives considered

No response

Importance

would make my life easier

Additional Information

No response

@GorvGoyl GorvGoyl added the ✨ Feature Request Something should be added label Jan 13, 2023
@GorvGoyl
Copy link
Author

there's this postcss lib that converts rem to px but it seems like it is not compatible with twind https://github.com/TheDutchCoder/postcss-rem-to-px

@sastan
Copy link
Collaborator

sastan commented Jan 13, 2023

You can transform the generated CSS using the finalize(rule) config option:

const config = {
	finalize(rule) {
		return {
			...rule,
			// d: the CSS declaration body
			// Based on https://github.com/TheDutchCoder/postcss-rem-to-px/blob/main/index.js
			d: rule.d.replace(/"[^"]+"|'[^']+'|url\([^)]+\)|(-?\d*\.?\d+)rem/g, (match, p1) => {
				if (p1 === undefined) return match
				return `${p1 * 16}${p1 == 0 ? '' : unit}`
			}),
		}
	}
})

This could be packaged as preset:

export default presetRemToPx({baseValue = 16} = {}) {
	return {
		finalize(rule) {
			return {
				...rule,
				// d: the CSS declaration body
				// Based on https://github.com/TheDutchCoder/postcss-rem-to-px/blob/main/index.js
				d: rule.d.replace(/"[^"]+"|'[^']+'|url\([^)]+\)|(-?\d*\.?\d+)rem/g, (match, p1) => {
					if (p1 === undefined) return match
					return `${p1 * baseValue}${p1 == 0 ? '' : unit}`
				}),
			}
		}
	}
}

And then:

export default defineConfig({
  presets: [presetRemToPx()],
})

@GorvGoyl
Copy link
Author

@sastan thank you so much, that worked!

@kavish-p
Copy link

this worked for me.
twind.config.js

import { defineConfig } from "@twind/core";
import presetAutoprefix from "@twind/preset-autoprefix";
import presetTailwind from "@twind/preset-tailwind";

const presetRemToPx = ({ baseValue = 16 } = {}) => {
  return {
    finalize(rule) {
      return {
        ...rule,
        d: rule.d?.replace(
          /"[^"]+"|'[^']+'|url\([^)]+\)|(-?\d*\.?\d+)rem/g,
          (match, p1) => {
            if (p1 === undefined) return match;
            return `${p1 * baseValue}${p1 == 0 ? "" : "px"}`;
          }
        ),
      };
    },
  };
};

export default defineConfig({
  presets: [presetAutoprefix(), presetTailwind(), presetRemToPx()],
});

@kishansripada
Copy link

where does finalize come from?

@kishansripada
Copy link

You can transform the generated CSS using the finalize(rule) config option:

const config = {
	finalize(rule) {
		return {
			...rule,
			// d: the CSS declaration body
			// Based on https://github.com/TheDutchCoder/postcss-rem-to-px/blob/main/index.js
			d: rule.d.replace(/"[^"]+"|'[^']+'|url\([^)]+\)|(-?\d*\.?\d+)rem/g, (match, p1) => {
				if (p1 === undefined) return match
				return `${p1 * 16}${p1 == 0 ? '' : unit}`
			}),
		}
	}
})

This could be packaged as preset:

export default presetRemToPx({baseValue = 16} = {}) {
	return {
		finalize(rule) {
			return {
				...rule,
				// d: the CSS declaration body
				// Based on https://github.com/TheDutchCoder/postcss-rem-to-px/blob/main/index.js
				d: rule.d.replace(/"[^"]+"|'[^']+'|url\([^)]+\)|(-?\d*\.?\d+)rem/g, (match, p1) => {
					if (p1 === undefined) return match
					return `${p1 * baseValue}${p1 == 0 ? '' : unit}`
				}),
			}
		}
	}
}

And then:

export default defineConfig({
  presets: [presetRemToPx()],
})

Hey! Thanks so much. I get "rule" is undefined when i try to use this. not sure where rule or finalize come from. Thanks!

@stale stale bot added the 🛑 Abandoned The issue or Pull Request will not be worked on label Nov 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🛑 Abandoned The issue or Pull Request will not be worked on ✨ Feature Request Something should be added
Projects
None yet
Development

No branches or pull requests

4 participants