Skip to content

Commit

Permalink
feat(rule): update border radius rules
Browse files Browse the repository at this point in the history
  • Loading branch information
zyyv committed Oct 30, 2024
1 parent b3f3d1c commit cdb25c9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 54 deletions.
7 changes: 4 additions & 3 deletions src/rules/position.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { EmptyKeyPlaceholder } from '../constants'
import type { AtomicComposed, DynamicPropAtomicMap, PropsAtomicMap, StaticPropAtomicMap } from '../types'

export const position = ['top', 'bottom', 'left', 'right']
export const positionShort = ['t', 'b', 'l', 'r']

export const positions: PropsAtomicMap[] = [
...position.map<StaticPropAtomicMap>((p, i) => [p, [p, positionShort[i]]]),
['position', EmptyKeyPlaceholder, { separator: '' }],
['z-index', 'z'],
]

export function createPos4Props(prop: string, atom: string, shortAtom?: string): DynamicPropAtomicMap {
return [
new RegExp(`^${prop}-(top|bottom|left|right)(?:-\\w+)?$`),
([, p]) => {
const index = position.indexOf(p)
if (index > -1) {
return [atom, shortAtom].filter(Boolean).map(s => `${s}-${positionShort[index]}`) as AtomicComposed
}
return [atom, shortAtom].filter(Boolean).map(s => `${s}-${positionShort[index]}`) as AtomicComposed
},
]
}
65 changes: 18 additions & 47 deletions src/rules/size.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { StaticPropAtomicMap } from '../types'
import { position, positionShort } from './position'
import type { PropsAtomicMap } from '../types'

export const sizes: StaticPropAtomicMap[] = [
export const sizes: PropsAtomicMap[] = [
['width', 'w'],
['height', 'h'],
['min-width', 'min-w'],
Expand All @@ -9,51 +10,21 @@ export const sizes: StaticPropAtomicMap[] = [
['max-height', 'max-h'],
['font-size', 'text'],
['line-height', ['leading', 'lh']],
['border-radius', ['rounded', 'rd']],
['border-top-left-radius', ['rounded-tl', 'rd-tl']],
['border-top-right-radius', ['rounded-tr', 'rd-tr']],
['border-bottom-right-radius', ['rounded-br', 'rd-br']],
['border-bottom-left-radius', ['rounded-bl', 'rd-bl']],
['border-width', ['border', 'b']],
[/^border(?:-(top|bottom|left|right))?(?:-(top|bottom|left|right))?-radius$/, ([, y, x]) => {
const pos = [y, x].filter(Boolean).map(p => positionShort[position.indexOf(p)])
const suffix = pos.length > 0 ? `-${pos.join('')}` : ''

return [
`rounded${suffix}`,
`rd${suffix}`,
]
}],

['border-top-width', 'btw'],
['border-right-width', 'brw'],
['border-bottom-width', 'bbw'],
['border-left-width', 'blw'],
['border-style', 'bs'],
['border-top-style', 'bts'],
['border-right-style', 'brs'],
['border-bottom-style', 'bbs'],
['border-left-style', 'bls'],
['border-color', 'bc'],
['border-top-color', 'btc'],
['border-right-color', 'brc'],
['border-bottom-color', 'bbc'],
['border-left-color', 'blc'],
['border', 'b'],
['outline', 'o'],
['outline-width', 'ow'],
['outline-style', 'os'],
['outline-color', 'oc'],
// ['border-top-left-radius', ['rounded-tl', 'rd-tl']],
// ['border-top-right-radius', ['rounded-tr', 'rd-tr']],
// ['border-bottom-right-radius', ['rounded-br', 'rd-br']],
// ['border-bottom-left-radius', ['rounded-bl', 'rd-bl']],
['border-width', ['border', 'b']],
['box-shadow', 'shadow'],
['text-shadow', 'tshadow'],
['letter-spacing', 'ls'],
['word-spacing', 'ws'],
['background-size', 'bg-size'],
['background-position', 'bg-pos'],
['background-repeat', 'bg-repeat'],
['background-attachment', 'bg-attach'],
['background-origin', 'bg-origin'],
['background-clip', 'bg-clip'],
['background-color', 'bgc'],
['background-image', 'bgi'],
['background-blend-mode', 'bg-blend'],
['background-attachment', 'bg-attach'],
['background-origin', 'bg-origin'],
['background-clip', 'bg-clip'],
['background-color', 'bgc'],
['background-image', 'bgi'],
['background-blend-mode', 'bg-blend'],
['background-attachment', 'bg-attach'],
['background-origin', ' bg-origin'],
['letter-spacing', 'tracking'],
]
25 changes: 21 additions & 4 deletions test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ exports[`analyzePared > transfrom parsed 1`] = `
"rounded-5px",
],
},
{
"css": "border-top-left-radius: 5px",
"shortTokens": [
"rd-tl-5px",
],
"tokens": [
"rounded-tl-5px",
],
},
{
"css": "box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1)",
"shortTokens": [
Expand Down Expand Up @@ -268,13 +277,21 @@ exports[`analyzePared > transfrom parsed 1`] = `
},
{
"css": "z-index: 10",
"shortTokens": undefined,
"tokens": undefined,
"shortTokens": [
"z-10",
],
"tokens": [
"z-10",
],
},
{
"css": "position: relative",
"shortTokens": undefined,
"tokens": undefined,
"shortTokens": [
"relative",
],
"tokens": [
"relative",
],
},
{
"css": "top: 10px",
Expand Down
1 change: 1 addition & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ describe('analyzePared', () => {
'margin-left: auto',
'margin-right: auto',
'border-radius: 5px',
'border-top-left-radius: 5px',
'box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1)',
'opacity: 0.8',
'transition: all 0.3s ease',
Expand Down

0 comments on commit cdb25c9

Please sign in to comment.