Giter Club home page Giter Club logo

valtiotextcolor's Introduction

Changing Tailwind text color class using Valtio, Next.js and Typescript

valtio



The Index Page contains the text 'Valtio Color Theme' which has a tailwind class variable for the text color along with the Color Wheel Component.

export default function Home() {
  const snap = useSnapshot(colorState)

  return (
    <div className="flex justify-evenly">
      <div
        className={`flex items-center h-96 justify-center text-2xl ${snap.colorClassName}`}
      >
        <h1>Valtio Color Theme</h1>
      </div>
      <ColorWheel />
    </div>
  )
}

The Color Wheel Component imports an enum containing fixed text color tailwind classes.

export enum Color {
  Red = 'text-red-500',
  Pink = 'text-pink-500',
  Rose = 'text-rose-500',
  Purple = 'text-purple-500',
  Fuchsia = 'text-fuchsia-500',
  Indigo = 'text-indigo-500',
  Blue = 'text-blue-500',
  Sky = 'text-sky-500',
  Cyan = 'text-cyan-500',
  Teal = 'text-teal-500',
  Green = 'text-green-500',
  Lime = 'text-lime-500',
  Yellow = 'text-yellow-500',
  Amber = 'text-amber-500',
  Orange = 'text-orange-500',
  Emerald = 'text-emerald-500',
}

which it then maps over them and displays them in a grid. When you click on each color it changes the tailwind class variable for the div with 'Valtio Color Theme' inside the index page

const wheel = Object.keys(Color).map((keyName) => (
  <div
    key={keyName}
    className={`${Color[keyName]} mx-2 flex items-center justify-center font-mono hover:cursor-pointer`}
    onClick={() => actions.setColorState(Color[keyName])}
  >
    {keyName}
  </div>
))

return <div className="grid grid-cols-4 gap-4 ">{wheel}</div>

The store.ts file contains the default state of the tailwind class variable name in the index page. It also contains an action function which acts as a setter for the tailwind class variable.

import { proxy } from 'valtio'

interface IColor {
  colorClassName: String;
}

export const colorState = proxy < IColor > { colorClassName: 'text-slate-500' }
export const actions = {
  setColorState(clickedColor: string) {
    colorState.colorClassName = clickedColor
  },
}

The Color Wheel Component imports the action function, which it then invokes onClick passing the tailwind class variable name of the clicked color as a parameter.

  onClick={() => actions.setColorState(Color[keyName])}

valtiotextcolor's People

Contributors

ichbtrv avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.