Skip to content

Instantly share code, notes, and snippets.

@wangpin34
Created November 10, 2023 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wangpin34/40f2a5508eaf375de69c7514a758d12f to your computer and use it in GitHub Desktop.
Save wangpin34/40f2a5508eaf375de69c7514a758d12f to your computer and use it in GitHub Desktop.
handwrite several classes of tailwindcss in unocss
/* eslint-disable no-useless-escape */
// uno.config.ts
import { defineConfig } from 'unocss'
function spaceToRem(space: string) {
const num = parseInt(space)
if (!isNaN(num)) {
return `${num * 0.25}rem`
}
return space
}
export default defineConfig({
presets: [],
rules: [
[/^m-\[([^\[\]]+)\]$/, ([, val]) => ({ margin: val })],
[/^mt-\[([^\[\]]+)\]$/, ([, val]) => ({ 'margin-top': val })],
[/^mb-\[([^\[\]]+)\]$/, ([, val]) => ({ 'margin-bottom': val })],
[/^my-\[([^\[\]]+)\]$/, ([, val]) => ({ 'margin-top': val, 'margin-bottom': val })],
[/^ml-\[([^\[\]]+)\]$/, ([, val]) => ({ 'margin-left': val })],
[/^mr-\[([^\[\]]+)\]$/, ([, val]) => ({ 'margin-right': val })],
[/^mx-\[([^\[\]]+)\]$/, ([, val]) => ({ 'margin-left': val, 'margin-right': val })],
[/^gap-\[([^\[\]]+)\]$/, ([,val]) => ({gap: val})],
[/^m-([\d]+)$/, ([,val]) => ({margin: spaceToRem(val)})],
[/^mt-([\d]+)$/, ([,val]) => ({'margin-top': spaceToRem(val)})],
[/^mb-([\d]+)$/, ([,val]) => ({'margin-bottom': spaceToRem(val)})],
[/^ml-([\d]+)$/, ([,val]) => ({'margin-left': spaceToRem(val)})],
[/^mr-([\d]+)$/, ([,val]) => ({'margin-right': spaceToRem(val)})],
[/^mx-([\d]+)$/, ([,val]) => ({'margin-left': spaceToRem(val), 'margin-right': spaceToRem(val)})],
[/^my-([\d]+)$/, ([,val]) => ({'margin-top': spaceToRem(val), 'margin-bottom': spaceToRem(val) })],
[/^gap-([\d]+)$/, ([,val]) => ({gap: spaceToRem(val)})],
['flex', { display: 'flex' }],
['flex-col', {'flex-direction': 'column'}],
['flex-row', {'flex-direction': 'row'}],
]
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment