Skip to content

Commit

Permalink
feat: ✨ adds built-in border width variables and parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Sullivan committed Oct 30, 2022
1 parent c5e5a29 commit 3db4f15
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
4 changes: 4 additions & 0 deletions css/core.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
--measure: 60ch;
--ratio: 1.25; /* Major Third */

--border-sm: .125rem;
--border-md: calc(var(--border-sm) * var(--ratio) * var(--ratio));
--border-lg: calc(var(--border-md) * var(--ratio) * var(--ratio));

--gap-4xs: calc(var(--gap-3xs) / var(--ratio));
--gap-3xs: calc(var(--gap-2xs) / var(--ratio));
--gap-2xs: calc(var(--gap-xs) / var(--ratio));
Expand Down
18 changes: 18 additions & 0 deletions src/borders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { isCssLength } from './css.js'
import type { CSSLength } from './css.js'

const Widths = {
'sm': 'var(--border-sm)',
'md': 'var(--border-md)',
'lg': 'var(--border-lg)'
}

export type BorderWidth = keyof typeof Widths | CSSLength | number

export function parseBorderWidth(width: BorderWidth): CSSLength {
return typeof width === 'number' ? `${width}px`
// @ts-ignore
: width in Widths ? Widths[width]
: isCssLength(width) ? width
: '0px'
}
2 changes: 1 addition & 1 deletion src/gaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type GapSize = keyof typeof Gaps

export type Gap = GapSize | CSSLength | number

export function parseGap(gap: Gap | unknown): CSSLength | undefined {
export function parseGap(gap: Gap | unknown): CSSLength {
if (typeof gap === 'number') return `${gap}px`

if (typeof gap === 'string') {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './borders.js'
export * from './colors.js'
export * from './css.js'
export * from './gaps.js'
Expand Down
27 changes: 27 additions & 0 deletions tests/borders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { suite } from 'uvu'
import * as assert from 'uvu/assert'
import { parseBorderWidth, BorderWidth } from '../src/borders.js'

const Borders = suite('borders')

console.log(parseBorderWidth('thin'))

Borders('CSS border variables', () => {
assert.type(parseBorderWidth, 'function', 'parseBorderWidth is a function')
assert.is(parseBorderWidth(10), '10px', 'converts numbers to pixels')
assert.is(parseBorderWidth('10vw'), '10vw', 'returns valid CSS lengths unchanged')
assert.is(parseBorderWidth('10abc' as any), '0px', 'ignores invalid CSS length strings')
assert.is(parseBorderWidth('thicker' as any), '0px', 'ignores unknown border width variables')

const cases: [BorderWidth, string, string][] = [
'sm',
'md',
'lg',
].map(size => [size, `var(--border-${size})`, `border size "${size}"`])

for (const [value, expected, message] of cases) {
assert.is(parseBorderWidth(value), expected, message)
}
})

Borders.run()

0 comments on commit 3db4f15

Please sign in to comment.