-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add smaller-than and larget-than mixins
- Loading branch information
Showing
6 changed files
with
170 additions
and
53 deletions.
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,95 @@ | ||
function scaleRem(remValue: string) { | ||
return `calc(${remValue} * var(--mantine-scale))`; | ||
} | ||
|
||
function createConverter(units: string, { shouldScale = false } = {}) { | ||
function converter(value: unknown): string { | ||
if (value === 0 || value === '0') { | ||
return '0'; | ||
} | ||
|
||
if (typeof value === 'number') { | ||
const val = `${value / 16}${units}`; | ||
return shouldScale ? scaleRem(val) : val; | ||
} | ||
|
||
if (typeof value === 'string') { | ||
if (value.startsWith('calc(') || value.startsWith('var(')) { | ||
return value; | ||
} | ||
|
||
if (value.includes(' ')) { | ||
return value | ||
.split(' ') | ||
.map((val) => converter(val)) | ||
.join(' '); | ||
} | ||
|
||
if (value.includes(units)) { | ||
return shouldScale ? scaleRem(value) : value; | ||
} | ||
|
||
const replaced = value.replace('px', ''); | ||
if (!Number.isNaN(Number(replaced))) { | ||
const val = `${Number(replaced) / 16}${units}`; | ||
return shouldScale ? scaleRem(val) : val; | ||
} | ||
} | ||
|
||
return value as string; | ||
} | ||
|
||
return converter; | ||
} | ||
|
||
const rem = createConverter('rem', { shouldScale: true }); | ||
const remNoScale = createConverter('rem'); | ||
const em = createConverter('em'); | ||
|
||
function getTransformedScaledValue(value: unknown) { | ||
if (typeof value !== 'string' || !value.includes('var(--mantine-scale)')) { | ||
return value; | ||
} | ||
|
||
return value | ||
.match(/^calc\((.*?)\)$/)?.[1] | ||
.split('*')[0] | ||
.trim(); | ||
} | ||
|
||
function px(value: unknown) { | ||
const transformedValue = getTransformedScaledValue(value); | ||
|
||
if (typeof transformedValue === 'number') { | ||
return transformedValue; | ||
} | ||
|
||
if (typeof transformedValue === 'string') { | ||
if (transformedValue.includes('calc') || transformedValue.includes('var')) { | ||
return transformedValue; | ||
} | ||
|
||
if (transformedValue.includes('px')) { | ||
return Number(transformedValue.replace('px', '')); | ||
} | ||
|
||
if (transformedValue.includes('rem')) { | ||
return Number(transformedValue.replace('rem', '')) * 16; | ||
} | ||
|
||
if (transformedValue.includes('em')) { | ||
return Number(transformedValue.replace('em', '')) * 16; | ||
} | ||
|
||
return Number(transformedValue); | ||
} | ||
|
||
return NaN; | ||
} | ||
|
||
module.exports = { | ||
px, | ||
em, | ||
rem, | ||
remNoScale, | ||
}; |
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
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,21 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`larger-than it transforms larger-than mixin correctly 1`] = ` | ||
" | ||
@media (min-width: 20em) { | ||
.a { | ||
background: red | ||
} | ||
} | ||
" | ||
`; | ||
|
||
exports[`smaller-than it transforms smaller-than mixin correctly 1`] = ` | ||
" | ||
@media (max-width: 19.99375em) { | ||
.a { | ||
background: red | ||
} | ||
} | ||
" | ||
`; |
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,31 @@ | ||
import { testTransform } from './utils'; | ||
|
||
const smallerThanInput = ` | ||
.a { | ||
@mixin smaller-than 320px { | ||
background: red; | ||
} | ||
} | ||
`; | ||
|
||
const largerThanInput = ` | ||
.a { | ||
@mixin larger-than 320px { | ||
background: red; | ||
} | ||
} | ||
`; | ||
|
||
describe('smaller-than', () => { | ||
it('it transforms smaller-than mixin correctly', async () => { | ||
const res = await testTransform(smallerThanInput); | ||
expect(res.css).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('larger-than', () => { | ||
it('it transforms larger-than mixin correctly', async () => { | ||
const res = await testTransform(largerThanInput); | ||
expect(res.css).toMatchSnapshot(); | ||
}); | ||
}); |