-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ adds built-in border width variables and parsing
- Loading branch information
Tony Sullivan
committed
Oct 30, 2022
1 parent
c5e5a29
commit 3db4f15
Showing
5 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |