Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat: add transition-safe-* tailwind utilities (#21958)

Charis5e878098f219
diff --git a/packages/config/tailwind.config.js b/packages/config/tailwind.config.js
index 2b22ef02a20..f5207f9a090 100644
--- a/packages/config/tailwind.config.js
+++ b/packages/config/tailwind.config.js
@@ -1,15 +1,16 @@
-const ui = require('./ui.config.js');
-const deepMerge = require('deepmerge');
+const ui = require('./ui.config.js')
+const deepMerge = require('deepmerge')
+const plugin = require('tailwindcss/plugin')
-const color = require('./../ui/build/css/tw-extend/color');
+const color = require('./../ui/build/css/tw-extend/color')
/**
*
*/
-let colorExtend = {};
+let colorExtend = {}
Object.values(color).map((x, i) => {
- colorExtend[ Object.keys(color)[ i ] ] = `hsl(${x.cssVariable} / <alpha-value>)`; // x.cssVariable
-});
+ colorExtend[Object.keys(color)[i]] = `hsl(${x.cssVariable} / <alpha-value>)` // x.cssVariable
+})
// console.log('colorExtend', colorExtend)
// console.log('colorExtend kebabToNested', kebabToNested(colorExtend))
@@ -21,16 +22,16 @@ Object.values(color).map((x, i) => {
* adds <alpha-value> as part of the hsl value
*/
function generateTwColorClasses(globalKey, twAttributes) {
- let classes = {};
+ let classes = {}
Object.values(twAttributes).map((attr, i) => {
- const attrKey = Object.keys(twAttributes)[ i ];
+ const attrKey = Object.keys(twAttributes)[i]
if (attrKey.includes(globalKey)) {
- const keySplit = attrKey.split('-').splice(1).join('-');
+ const keySplit = attrKey.split('-').splice(1).join('-')
let payload = {
- [ keySplit ]: `hsl(${attr.cssVariable} / <alpha-value>)`,
- };
+ [keySplit]: `hsl(${attr.cssVariable} / <alpha-value>)`,
+ }
if (keySplit == 'DEFAULT') {
// includes a 'default' duplicate
@@ -38,48 +39,48 @@ function generateTwColorClasses(globalKey, twAttributes) {
payload = {
...payload,
default: `hsl(${attr.cssVariable} / <alpha-value>)`,
- };
+ }
}
classes = {
...classes,
...payload,
- };
+ }
}
- });
+ })
/**
* mutate object into nested object for tailwind theme structure
*/
- const nestedClasses = kebabToNested(classes);
+ const nestedClasses = kebabToNested(classes)
// return, but nest the keys if they are kebab case named
- return nestedClasses;
+ return nestedClasses
}
/**
* Helper to convert kebab named keys in object to nested nodes
*/
function kebabToNested(obj) {
- const result = {};
- for (const [ key, value ] of Object.entries(obj)) {
- const parts = key.split('-');
- let currentObj = result;
+ const result = {}
+ for (const [key, value] of Object.entries(obj)) {
+ const parts = key.split('-')
+ let currentObj = result
for (let i = 0; i < parts.length; i++) {
- const part = parts[ i ] === 'DEFAULT' ? parts[ i ] : parts[ i ].toLowerCase(); // convert key to lowercase
- if (!currentObj[ part ]) {
- currentObj[ part ] = {};
+ const part = parts[i] === 'DEFAULT' ? parts[i] : parts[i].toLowerCase() // convert key to lowercase
+ if (!currentObj[part]) {
+ currentObj[part] = {}
}
if (i === parts.length - 1) {
if (typeof value === 'object') {
- currentObj[ part ] = kebabToNested(value); // recursively convert nested objects
+ currentObj[part] = kebabToNested(value) // recursively convert nested objects
} else {
- currentObj[ part ] = value.toString().toLowerCase(); // convert value to lowercase
+ currentObj[part] = value.toString().toLowerCase() // convert value to lowercase
}
} else {
- currentObj = currentObj[ part ];
+ currentObj = currentObj[part]
}
}
}
- return result;
+ return result
}
/**
@@ -406,14 +407,94 @@ const uiConfig = ui({
// shadcn defaults END
},
},
- plugins: [require('@tailwindcss/typography'), require('tailwindcss-animate')],
+ plugins: [
+ require('@tailwindcss/typography'),
+ require('tailwindcss-animate'),
+ plugin(motionSafeTransition),
+ ],
})
+/**
+ * Plugin to add `safe` versions of the `transition-*` properties, which respect
+ * `prefers-reduced-motion`.
+ *
+ * When users prefer reduced motion, the duration of transform transitions is
+ * reduced to something negiglible (1ms). The original `transition-*` properties
+ * aren't overridden to provide flexibility, in situations where you want to
+ * handle the `prefers-reduced-motion` case some other way.
+ *
+ * See https://css-tricks.com/levels-of-fix/.
+ *
+ * Usage: <div className="transition-safe duration-safe-100">
+ * - Transitioned properties will animate with duration 100, _except_
+ * transform properties when prefers-reduced-motion is on, which
+ * will animate instantaneously.
+ *
+ * Note:
+ * - `duration-safe` must be used with `transition-safe`
+ * - Non-safe `duration` must be used with non-safe `transition`
+ * - (Cannot be mixed)
+ */
+function motionSafeTransition({ addUtilities, matchUtilities, theme }) {
+ addUtilities({
+ '.transition-safe': {
+ transitionProperty:
+ 'color, transform, background-color, border-color, text-decoration-color, fill, stroke, opacity, box-shadow, filter, backdrop-filter',
+ transitionTimingFunction: 'cubic-bezier(0.4, 0, 0.2, 1)',
+ transitionDuration: '150ms',
+ '@media (prefers-reduced-motion)': {
+ transitionDuration:
+ '150ms, 1ms, 150ms, 150ms, 150ms, 150ms, 150ms, 150ms, 150ms, 150s, 150ms',
+ },
+ },
+ '.transition-safe-all': {
+ transitionProperty: 'all, transform',
+ transitionTimingFunction: 'cubic-bezier(0.4, 0, 0.2, 1)',
+ transitionDuration: '150ms',
+ '@media (prefers-reduced-motion)': {
+ transitionDuration: '150ms, 1ms',
+ },
+ },
+ '.transition-safe-transform': {
+ /**
+ * The duplicate `transform` here is a hacky way of dealing with the fact
+ * that `transform` must be second in `transition-safe-all` to override
+ * `all`, and its order must be the same across all `transition-safe-*`
+ * classes, so the proper duration applies in `duration-safe`.
+ */
+ transitionProperty: 'transform, transform',
+ transitionTimingFunction: 'cubic-bezier(0.4, 0, 0.2, 1)',
+ transitionDuration: '150ms',
+ '@media (prefers-reduced-motion)': {
+ transitionDuration: '1ms',
+ },
+ },
+ })
+
+ matchUtilities(
+ {
+ 'duration-safe': (value) => ({
+ transitionDuration: value,
+ '@media (prefers-reduced-motion)': {
+ /**
+ * Preserves the indicated duration for everything except `transform`.
+ *
+ * Relies on browsers truncating the `transition-duration` property
+ * if there are more values than there are transitioned properties.
+ */
+ transitionDuration: `${value}, 1ms, ${value}, ${value}, ${value}, ${value}, ${value}, ${value}, ${value}, ${value}, ${value}`,
+ },
+ }),
+ },
+ { values: theme('transitionDuration') }
+ )
+}
+
function arrayMergeFn(destinationArray, sourceArray) {
return destinationArray.concat(sourceArray).reduce((acc, cur) => {
- if (acc.includes(cur)) return acc;
- return [ ...acc, cur ];
- }, []);
+ if (acc.includes(cur)) return acc
+ return [...acc, cur]
+ }, [])
}
/**
@@ -424,7 +505,7 @@ function arrayMergeFn(destinationArray, sourceArray) {
function wrapper(tailwindConfig) {
return deepMerge({ ...tailwindConfig }, uiConfig, {
arrayMerge: arrayMergeFn,
- });
+ })
}
-module.exports = wrapper;
+module.exports = wrapper