From 096fe8f1ce662091b8e8090e2de06bcd71e15c6c Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Fri, 9 Sep 2022 18:00:00 +0100 Subject: [PATCH] refactor: use a multi-theme plugin for colours --- pages/index.js | 4 +-- styles/globals.css | 28 +-------------- tailwind.config.js | 89 ++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 81 insertions(+), 40 deletions(-) diff --git a/pages/index.js b/pages/index.js index 1760840..57b69c4 100644 --- a/pages/index.js +++ b/pages/index.js @@ -6,7 +6,7 @@ export default function Home() {

Featured case study

-
+
@@ -42,7 +42,7 @@ export default function Home() {

Explore this event diff --git a/styles/globals.css b/styles/globals.css index 738aa66..96f2af7 100644 --- a/styles/globals.css +++ b/styles/globals.css @@ -3,22 +3,6 @@ @tailwind utilities; @layer components { - .button { - @apply px-5 py-2 inline-block text-lg font-bold border-2 duration-200 transition-colors ease-in-out; - } - - [data-theme="blue"] .button--secondary { - @apply bg-red border-red text-white hover:bg-white hover:text-red; - } - - [data-theme="purple"] .button--secondary { - @apply bg-orange border-orange text-purple hover:bg-white hover:text-purple hover:border-purple; - } - - [data-theme="teal"] .button--secondary { - @apply bg-pink border-pink text-white hover:bg-white hover:text-pink; - } - .fancy-corners { position: relative; background-color: #fff; @@ -30,21 +14,11 @@ position: absolute; width: 8em; height: 8em; + background-color: theme('colors.primary'); background-size: 100% 100%; background-position: 0 0; transform: translateZ(-1px); z-index: -1; - @apply bg-purple; - } - - [data-theme="blue"] .fancy-corners:after, - [data-theme="blue"] .fancy-corners:before { - @apply bg-blue; - } - - [data-theme="teal"] .fancy-corners:after, - [data-theme="teal"] .fancy-corners:before { - @apply bg-teal; } .fancy-corners[class*="--bottom"]:after, diff --git a/tailwind.config.js b/tailwind.config.js index ccfd663..7312015 100644 --- a/tailwind.config.js +++ b/tailwind.config.js @@ -1,3 +1,73 @@ +let plugin = require("tailwindcss/plugin"); + +let colors = { + black: "#333333", + blue: "#334982", + grey: "#f3f3f3", + orange: "#fdb913", + pink: "#e40087", + purple: "#782b8f", + red: "#dd372f", + teal: "#00857d", + white: "#fff", +}; + +let themes = { + blue: { + quaternary: colors.red, + quinary: colors.red, + primary: colors.blue, + secondary: colors.red, + tertiary: colors.white, + }, + + purple: { + quaternary: colors.white, + quinary: colors.purple, + primary: colors.purple, + secondary: colors.orange, + tertiary: colors.purple, + }, + + teal: { + quaternary: colors.pink, + quinary: colors.pink, + primary: colors.teal, + secondary: colors.pink, + tertiary: colors.white, + }, +}; + +let multiThemePlugin = plugin(function ({ addBase }) { + addBase(generateForRoot()); + + Object.keys(themes).forEach((themeName) => { + addBase(generateForTheme(themeName)); + }); +}); + +function generateForRoot() { + let defaultThemeName = Object.keys(themes)[0]; + + return Object.entries(themes[defaultThemeName]).map(([colourName, value]) => { + return { + ":root": { + [`--color-${colourName}`]: value, + }, + }; + }); +} + +function generateForTheme(themeName) { + return Object.entries(themes[themeName]).map(([colourName, value]) => { + return { + [`[data-theme=${themeName}]`]: { + [`--color-${colourName}`]: value, + }, + }; + }); +} + /** @type {import('tailwindcss').Config} */ module.exports = { content: [ @@ -6,17 +76,14 @@ module.exports = { ], theme: { colors: { - black: "#333333", - blue: "#334982", - grey: "#f3f3f3", - orange: "#fdb913", - pink: "#e40087", - purple: "#782b8f", - red: "#dd372f", - teal: "#00857d", - white: "#fff", + ...colors, + + quaternary: "var(--color-quaternary)", + quinary: "var(--color-quinary)", + primary: "var(--color-primary)", + secondary: "var(--color-secondary)", + tertiary: "var(--color-tertiary)", }, - extend: {}, }, - plugins: [], + plugins: [multiThemePlugin], };