how to create a tailwind responsive class using a plugin - tailwind-css

how can I reproduce the following scss using a plugin?
.large-title {
font-family: $font-family-2;
#apply leading-none;
color: $large-title-color;
#apply uppercase;
#apply text-xl;
// I don't know how to add these in the plugin
#apply sm:text-2xl;
#apply md:text-3xl;
#apply lg:text-4xl;
#apply xl:text-5xl;
}
in my config file, I currently have:
module.exports = {
[...]
plugins: [
require('#tailwindcss/forms'),
plugin(function({ addBase, config }) {
addBase({
'.large-title': {
fontFamily: [config('theme.fontFamily.family-2')],
fontSize: config('theme.fontSize.xl'),
lineHeight: config('theme.lineHeight.none'),
color: config('theme.colors.primary.900'),
textTransform: 'uppercase',
},
})
})
],
}
The only thing missing is I can't find out how to add the responsive size changes from the class in the plugin.

One way to add #media queries to a class in Tailwind's CSS-in-JS syntax is nesting:
addBase({
'.large-title': {
'#media (min-width: 500px)': {
fontSize: theme('fontSize.xl'),
}
},
})
Additionally, user's breakpoints can be accessed using the theme() function:
const sm = theme('screens.sm', {})
Combining these two snippets you could:
const sm = theme('screens.sm', {})
addBase({
'.large-title': {
`#media (min-width: ${sm})`: {
fontSize: theme('fontSize.xl'),
}
},
})
One caveat to be aware of in case you're building this plugin for public use is that a user may entirely change their theme.screens config. If a user removes the theme.screens.sm key from their configurations, for example, the snippet above would no longer work.

Related

How to see all values in the user’s theme configuration

I am writing plugin to add custom styles in TailwindCSS, but I am a bit confused with values of theme(). I don't know how to see all the theme values ​​to apply my base styles. How do I know where it is?
module.exports = {
// ...
plugins: [
plugin(function ({ addBase, addComponents, addUtilities, theme }) {
addBase({
'h1': {
fontSize: theme('fontSize.2xl'), // need to see more fontSize and properties, where I can see it?
},
'h2': {
fontSize: theme('fontSize.xl'),
},
})
}
]
}

Is there a way to add translate-z-[ ] utility class

I would like to add translate-z-[] utility class to my tailwindcss classes so I could use parallax scrolling effects with perspective and translateZ, is there a way to generate these classes (add something like --tw-translate-z variable at the end on tailwinds transform class)?
Yes, you can create plugins for custom utilities
You config should have something like this
const plugin = require('tailwindcss/plugin');
module.exports = {
theme: {},
plugins: [
plugin(function({ matchUtilities, theme }) {
matchUtilities(
{
'translate-z': (value) => ({
'--tw-translate-z': value,
transform: ` translate3d(var(--tw-translate-x), var(--tw-translate-y), var(--tw-translate-z)) rotate(var(--tw-rotate)) skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y))`,
}), // this is actual CSS
},
{ values: theme('translate'), supportsNegativeValues: true }
)
})
],
}
DEMO

Mui 5 and styled components - Problem with overriding theme base values

I'm using Mui5 with SC and I have issues with overriding theme base values.
I have base theme like this for base mui components, for example:
const theme = createTheme({
components: {
MuiButton: {
styleOverrides: {
root: {
borderRadius: "1.75rem",
color: "red",
}
}
}
}
});
And now I like to extend the base button with custom colors like this with SC:
const StyledButton = styled(Button)(({ theme }: { theme: Theme }) => {
return {
fontFamily: theme.xxx,
color: "green"
};
});
This is not working, only the theme defaults are applied.
What's wrong with the implementation or what I'm missing?
BR,
Are you using any other package such as material-ui.
Are all your packages also up to date and have these installed:
"#emotion/css": "^11.9.0",
"#emotion/react": "^11.9.3",
"#emotion/styled": "^11.9.3",
"#mui/material": "^5.9.1",
"#mui/styled-engine": "^5.8.7",
"#mui/styles": "^5.9.1",

How do I add a Media Query to a React CSS object?

Currently I have this:
const ButtonStyles = {
color: 'red',
}
But I want to add a Media Query for mobile, would it look something like this?:
const ButtonStyles = {
color: 'red',
'#media (max-width: 900px)': {
color: 'blue',
}
}
If the syntax is not in this format, then how is it supposed to be done?
Give a try.
https://github.com/kasinskas/react-native-media-query
Work just like RN StyleSheet, but with queries.
https://github.com/yocontra/react-responsive
Work with functions inside regular RN StyleSheet
An eg. with natives StyleSheet, Platform and React-Responsive and variables.
import { StyleSheet, Platform } from 'react-native';
import { useMediaQuery } from 'react-responsive';
export const STYLES = StyleSheet.create({
content: (setup) => {
const justifyTo = setup?.justify ? setup.justify : 'flex-start';
return {
justifyContent: justifyTo,
...Platform.select({
web: {
width: useMediaQuery({ maxWidth: 767 }) ? '100vw' : '70vw',
maxWidth: 1280,
marginHorizontal: 'auto',
marginVertical: 'auto',
},
}),
};
}
});
According to this discussion You cannot use Media queries with react css objects. You must use stylesheets with class names or other external modules like styled components.

React JSS style global

I'm using React-jss to set up global style. For example:
export default {
'#global': {
body: {
margin: 0,
fontFamily: '"Lobster", cursive, Roboto, "sans-serif"',
},
},
};
Code above works for me but when I want to style for css like this it not working.
export default {
'#global': {
body: ...,
'& *:before, & *:after': {
boxSizing: 'border-box',
},
},
};
I've searched on their github issues, on google, on their docs but still not find any answer for this question. Pls help me, thank you guys

Resources