Can I create a custom class with arbitrary values Tailwindcss - tailwind-css

I want to create a class in Tailwindcss with arbitrary values
for example
HTML
<button class="my-custom-class[#fff]" />
CSS
#layer componenets {
.my-custom-class {
#apply bg-[--here-arbitrary-value]; /** the value is #fff */
}
}

You can but not using layer components
You need to write simple plugin for that
tailwind.config.js
const plugin = require('tailwindcss/plugin')
const flattenColorPalette = require('tailwindcss/lib/util/flattenColorPalette')
module.exports = {
theme: {
extend: {
colors: {
from: {
config: {
500: 'yellow', // for example purposes
},
},
},
},
},
plugins: [
plugin(function ({ matchUtilities, theme }) {
matchUtilities(
{
// Class name
'my-custom-class': (value) => {
return {
backgroundColor: value, // Desired CSS properties here
color: theme('colors.white') // Just for example non-dynamic value
}
},
},
// Default values.
// `flattenColorPalette` required to support native Tailwind color classes like `red-500`, `amber-300`, etc.
// In most cases you may just pass `theme('config-key')`, where `config-key` could be any (`spacing`, `fontFamily`, `foo`, `bar`)
{ values: flattenColorPalette(theme('colors')) }
)
}),
],
}
Now you can use it like
<div class="my-custom-class-[#000] my-4 p-6">JIT as HEX</div>
<div class="my-custom-class-[red] my-4 p-6">JIT as string</div>
<div class="my-custom-class-[rgb(0,0,0)] my-4 p-6">JIT as RGB</div>
<div class="my-custom-class-blue-500 my-4 p-6">Using Tailwind Colors</div>
<div class="my-custom-class-from-config-500 my-4 p-6 text-black">Using extended colors from config. NOTE: `text-black` doesn't applied!</div>
NOTE: your class will take precedence over basic Tailwind classes
<div class="bg-red-500 my-custom-class-[yellow]">
This has yellow background
</div>
More about writing plugins here
DEMO

Related

adding border bottom color on react styled element

I'm working on a basic calculator app with dynamic themes to be applied. How do I add a dynamic style react border-bottom with a set color on the keyStyle constant to be applied on my buttons?
I can't pass the value of currentTheme.numKeyShadow in the borderBottom css style as it's already a string.
How do i go about this?
This is a snippet my code:
import { useStateContext } from '../context/contextProvider'
const NumKeys = () => {
const { currentTheme, SetCurrentTheme } = useStateContext()
const keysStyle = {
borderRadius: "8px",
borderBottom:"4px solid",
borderBottomColor: currentTheme.numkeyShadow,
backgroundColor: currentTheme.keysBackground
}
return (
<section>
<div className='numKeys'>
<button style={keysStyle}>1</button>
</div>
</section>
)
}
export default NumKeys
This is my part of my data source:
export default [
{
"id": 0,
"background": "#3a4764",
"keysBackground": "#232c43",
"screenBackground": "#182034",
"KeysBackground": "#637097",
"KeysShadow": "#404e72",
"equaKeyBackground": "#d03f2f",
"equalKeyShadow": "#93261a",
"numKeyBackground": "#eae3dc",
"numKeyShadow": "#b4a597",
"num": "444b5a",
"equal": "#ffff"
}
]
You don't need a context or custom hook here. The CSS for the theme is a constant, and hence, you can declare, import and export like a simple JavaScript file.
NumKeys.js (React component)
import theme from "./Theme";
const NumKeys = () => {
const keysStyle = {
borderRadius: "8px",
borderBottom: "4px solid",
borderBottomColor: theme.numKeyShadow,
backgroundColor: theme.keysBackground,
};
return (
<section>
<div className="numKeys">
<button style={keysStyle}>1</button>
</div>
</section>
);
};
export default NumKeys;
Theme.js
const theme = {
id: 0,
background: "#3a4764",
keysBackground: "#232c43",
screenBackground: "#182034",
KeysBackground: "#637097",
KeysShadow: "#404e72",
equaKeyBackground: "#d03f2f",
equalKeyShadow: "#93261a",
numKeyBackground: "#eae3dc",
numKeyShadow: "#b4a597",
num: "444b5a",
equal: "#ffff",
};
export default theme;
This would add the style in your React component.
Live version - https://codesandbox.io/s/theme-0t9773

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

Why is the custom color in tailwind not defined in NextJS production stage

I created a custom color on tailwind in next js. On localhost the defined color appears fine, but when I deploy to vercel the color doesn't appear.
here's the picture localhost
production in vercel
tailwind.config.js
const colors = require('tailwindcss/colors');
module.exports = {
purge: [
'./pages/**/*.{js,ts,jsx,tsx}',
'./components/**/*.{js,ts,jsx,tsx}'
],
darkMode: false, // or 'media' or 'class'
theme: {
colors: {
transparent: 'transparent',
current: 'currentColor',
black: {
DEFAULT: '#23232D'
},
white: colors.white,
gray: {
DEFAULT: '#A1A1A1',
},
...
}
},
variants: {
extend: {},
},
plugins: [],
}
ButtonColor/index.js
import PropTypes from 'prop-types';
import { motion } from 'framer-motion';
function ButtonColor({ color, isOpen, onClick }) {
const variants = {
open: { width: '100%' },
closed: { width: '50%' },
}
return (
<motion.div
className={`bg-${color} h-6 cursor-pointer`}
onClick={onClick}
animate={isOpen ? "open" : "closed"}
variants={variants}
>
</motion.div>
)
}
ButtonColor.propTypes = {
color: PropTypes.string.isRequired,
isOpen: PropTypes.bool.isRequired,
onClick: PropTypes.func.isRequired,
}
export default ButtonColor;
Any solutions for this case? thanks.
You can't use string concatenation to create CSS class names because PurgeCSS won't know to preserve your custom classes during the build process.
className={`${color === 'red' ? 'bg-red' : 'bg-blue'} h-6 cursor-pointer`}
Alternatively, you can create a custom global CSS/SCSS file. In that file, define your styles using tailwindcss directives.
global.{css|scss}
.button {
#apply h-6;
#apply cursor-pointer;
&.red{
#apply bg-red-700 dark:bg-red-900;
#apply text-white;
#apply hover:bg-red-800 dark:hover:bg-red-800;
}
&.gray {
#apply bg-gray-300 dark:bg-gray-600;
#apply text-gray-900 dark:text-gray-200;
#apply hover:bg-gray-400 dark:hover:bg-gray-500;
}
}
<motion.button className="button"> ...
Side note - motion.div should be motion.button

Vue.js how can i loop throw an array to add components to the dom according to array items

I am making an app that communicate with an api and fetch data,home page changes every day so i can't just add static components to it,
i need to create it according to the data that comes from the api.
i have a component for the home page called Home.vue
this component can have one or more Carousels depending on the data that i'am fetching.
i also have Carousel.vue which is responsible about displaying images and it had it's own props.
the question is :
How to add component to the dom from loop
this is Home.vue where i am making the loop :
<template>
<div>
<!--I Need The Loop right here-->
</div>
</template>
<script>
export default {
components: {},
data() {
return {
page_content: [],
widgets: [],
}
},
created() {
this.getHomeContent();
},
methods:
{
getHomeContent() {
window.axios.get(window.main_urls["home-content"]).then(response => {
this.page_content = JSON.parse(JSON.stringify(response.data));
console.log(this.page_content);
for (let index in this.page_content) {
switch (this.page_content[index].type) {
// if type is banner
case 'banner':
switch (this.page_content[index].display) {
// if display is carousel
case 'carousel':
console.log('carousel')
// end if display is carousel
this.widgets.push({
'type': 'Carousel',
'images': this.page_content[index].items,
})
}
// end if type is banner
}
}
});
}
}
}
</script>
and this is Carousel.vue which i need to be imported when needed with passing props :
<template>
<div>
<div >
<VueSlickCarousel>
<div v-for="image in images">
<img src="{{img}}">
</div>
</VueSlickCarousel>
</div>
</div>
</template>
<script>
import VueSlickCarousel from 'vue-slick-carousel'
import 'vue-slick-carousel/dist/vue-slick-carousel.css'
import 'vue-slick-carousel/dist/vue-slick-carousel-theme.css'
export default
{
components: {VueSlickCarousel},
name:'Carousel',
props:[
'images'
],
methods:
{
}
}
</script>
how to add Carousel.vue component to Home.vue dynamically some thing like:
if(data.display == 'carousel')
{
<carousel images="data.images"></carousel>
}
Import the component to your Home.vue :
import Carousel from './Carousel.vue'
export default {
components: {Carousel},
}
Then loop in your template:
<carousel v-for="(widget,index) in widgets" :key="index" :images="widget.images"/>
Best to use a widget.id rather than index for the key prop
This is the correct answer !
<template>
<div>
<template v-for="widget in widgets">
<div v-if="widget.type == 'carousel'" :key="widget.type">
<carousel
:images="widget.images"
:arrows ="widget.arrows"
:dots = "widget.dots"
>
</carousel>
</div>
</template>
</div>
</template>
<script>
import Carousel from './widgets/Carousel.vue'
export default {
components: {Carousel},
data() {
return {
page_content: [],
widgets: [],
}
},
created() {
this.getHomeContent();
},
methods:
{
getHomeContent() {
window.axios.get(window.main_urls["home-content"]).then(response => {
this.page_content = JSON.parse(JSON.stringify(response.data));
console.log(this.page_content);
for (let index in this.page_content) {
switch (this.page_content[index].type) {
// if type is banner
case 'banner':
switch (this.page_content[index].display) {
// if display is carousel
case 'carousel':
console.log('carousel')
// end if display is carousel
this.widgets.push({
'type': 'carousel',
'arrows':true,
'dots':true,
'images': this.page_content[index].items,
})
}
// end if type is banner
}
}
});
}
}
}
</script>

How to Overwrite CSS of an external [third-party] component using Material-UI-React?

I'm trying to overwrite the default CSS of an external component which isn't developed in Material-UI or my project. In styled-components, I can just take the root classes and replace them with my custom CSS. How do I do the same with Material-UI-React?
.ace-tm .ace_variable {
color : red
}
Suppose I've to replace those two classes with the new color property, how do I do it in Material styles?
This is what I've tried with no luck!
const Styles = {
" & ace-tm": {
"& ace_variable": {
color: red,
fontSize: "16px"
},
}
};
I'm using withStyles to later inject them in the components.
I just found this and thought I'd share the solution for posterity:
const GlobalCss = withStyles((theme) => ({
'#global': {
'.ace-tm .ace_variable': {
color: 'red',
},
},
}))(() => null)
const SomeComponent = () => {
return (
<>
<GlobalCss />
<h1>Hey Jude</h1>
<SomeComponentWhoseCSSWillBeModified />
</>
}
Read more on this here: https://material-ui.com/styles/advanced/#global-css

Resources