-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Todo] percent-range() -> decimal-range() #267
base: main
Are you sure you want to change the base?
Changes from all commits
3310d5f
2ca3c19
f5e0681
dcc2fc6
c684113
ed863a2
0e153b0
8ed9abf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// prettier-ignore | ||
import Duomo from "./runtime" | ||
|
||
export function localStoragePreference() { | ||
if (!(Duomo.localStorageKey in window.localStorage)) { | ||
return null | ||
} | ||
const value = window.localStorage[Duomo.localStorageKey] | ||
if (value !== "light" && value !== "dark") { | ||
return null | ||
} | ||
return value | ||
} | ||
|
||
// prettier-ignore | ||
export function matchMediaPreference() { | ||
if (!("matchMedia" in window)) { | ||
return null | ||
} | ||
const matches = window.matchMedia("(prefers-color-scheme: dark)").matches | ||
const value = ({ | ||
false: "light", | ||
true: "dark", | ||
} as { [key: string]: string })["" + matches] | ||
return value | ||
} | ||
|
||
// prettier-ignore | ||
export function isKeyDownDarkMode(e: KeyboardEvent) { | ||
const ok = ( | ||
!e.ctrlKey && | ||
!e.altKey && | ||
(e.key.toLowerCase() === "`" || e.keyCode === 192) | ||
) | ||
return ok | ||
} | ||
|
||
// prettier-ignore | ||
export function isKeyDownDebugMode(e: KeyboardEvent) { | ||
const ok = ( | ||
e.ctrlKey && | ||
!e.altKey && | ||
(e.key.toLowerCase() === "`" || e.keyCode === 192) | ||
) | ||
return ok | ||
} | ||
|
||
// prettier-ignore | ||
export function parseDurationMs(durStr : string){ | ||
let [numStr, unit] = durStr.trim().split(/(ms|s|m)$/); | ||
if(!numStr){ | ||
throw new Error(`parseDurationMs: expected \`ms\`, \`s\`, or \`m\`; durStr=${durStr}`) | ||
} | ||
|
||
let n = +numStr; | ||
switch (unit) { | ||
case 'ms': | ||
// No-op | ||
break; | ||
case 's': | ||
n *= 1000; | ||
break; | ||
case 'm': | ||
n *= 1000 * 60; | ||
break; | ||
default: | ||
// No-op | ||
break; | ||
} | ||
return n; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,14 +12,6 @@ $-translate-range: ( | |
100, | ||
); | ||
|
||
// TODO: Change to `percent-range`. | ||
// | ||
// prettier-ignore | ||
$-scale-range: ( | ||
0, 0.1, 0.2, 0.25, 0.3, 0.4, 0.5, 0.6, 0.7, 0.75, 0.8, 0.9, 1, | ||
1.1, 1.2, 1.25, 1.3, 1.4, 1.5, 1.6, 1.7, 1.75, 1.8, 1.9, 2, | ||
); | ||
|
||
$-origin-opts: ( | ||
top: top, | ||
top-right: top right, | ||
|
@@ -63,7 +55,17 @@ $-origin-opts: ( | |
} | ||
} | ||
|
||
@each $rv in $-scale-range { | ||
@each $rv in get.decimal-range(0, 1) { | ||
// $rv : 0, 0.1, 0.2, 0.25, 0.3, 0.4, 0.5, 0.6, 0.7, 0.75, 0.8, 0.9, 1, | ||
.scale-#{get.abs($rv * 100)} { | ||
@include mixins.variants($composed...) { | ||
--transform-scale: #{$rv}; | ||
} | ||
} | ||
} | ||
|
||
@each $rv in get.decimal-range(1, 1) { | ||
// $rv : 1.1, 1.2, 1.25, 1.3, 1.4, 1.5, 1.6, 1.7, 1.75, 1.8, 1.9, 2, | ||
.scale-#{get.abs($rv * 100)} { | ||
@include mixins.variants($composed...) { | ||
--transform-scale: #{$rv}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
My get.decimal-range(0.1) // 0, 0.1, 0.2, 0.25, 0.3, 0.4, 0.5, 0.6, 0.7, 0.75, 0.8, 0.9, 1 So I created two different And I want to combine these two @for $i from 0 through 1 {
@each $rv in get.decimal-range(0, $i) {
.scale-#{get.abs($rv * 100)} {
@include mixins.variants($composed...) {
--transform-scale: #{$rv};
}
}
} Or is there better way to do it making the values from 0 to 2? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well the basic idea is that we can do something like |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
@use "../configuration" as conf; | ||
@use "../utils"; | ||
@use 'sass:list'; | ||
|
||
// TODO: Add support for `bar`? | ||
// Then we can have `bar` and `cap`. | ||
|
@@ -52,3 +53,16 @@ | |
@function percent-range() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In theory we can delete |
||
@return conf.$percent-range; | ||
} | ||
|
||
@function decimal-range($begin, $multiply) { | ||
$list: (); | ||
@each $range in conf.$percent-range { | ||
$list: list.append($list, $range * $multiply); | ||
@if ($begin) { | ||
$list: list.append($list, ($range * $multiply) + $begin); | ||
} @else { | ||
$list: list.append($list, $range * $multiply); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I rename // range example There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks good. We should write some tests to make sure this works exactly as we expect. Oh I just noticed you wrote some tests! 😆 Congrats! |
||
} | ||
@return $list; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
* @jest-environment node | ||
*/ | ||
declare function sass(data: string): string | ||
declare function errSass(testStr: string): Function | ||
|
||
test("integration", () => { | ||
const css = sass(` | ||
|
@@ -49,3 +50,12 @@ $end: -1; | |
} | ||
`.trim()) | ||
}) | ||
|
||
//test decimal-range | ||
test("integration", () => { | ||
expect(errSass(`decimal-range(0, 0.1)`)).toThrowError("0 0.01 0.02 0.025 0.03 0.04 0.05 0.06 0.07 0.075 0.08 0.09 0.1") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is great. I would just add some code to check that this works the same for negative values. Nice work! |
||
expect(errSass(`decimal-range(0, 1)`)).toThrowError("0 0.1 0.2 0.25 0.3 0.4 0.5 0.6 0.7 0.75 0.8 0.9 1") | ||
expect(errSass(`decimal-range(0, 10)`)).toThrowError("0 1 2 2.5 3 4 5 6 7 7.5 8 9 10") | ||
expect(errSass(`decimal-range(0, 100)`)).toThrowError("0 10 20 25 30 40 50 60 70 75 80 90 100") | ||
expect(errSass(`decimal-range(1, 1)`)).toThrowError("1 1.1 1.2 1.25 1.3 1.4 1.5 1.6 1.7 1.75 1.8 1.9 2") | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see what you did here. There’s a small utility you can use to combine these loops. So you can use
utils.list-of
(I think that’s what it’s called, you can double-check theutils
folder). It will create a new list from two separate lists. So you should be able to refactor this to be like:utils.list-of(get.decimal-range(0, -0.1), get.decimal-range(0, 0.1))
and that will enable you to only have to have one loop instead of two.The
get.n()
function just tells you in a number is negative or positive. So if we have separate loops, we have to haveget.n()
for the negative loop. So you don’t to worry about negative values when you combine loops. That is handled automatically.