Skip to content

Commit e487108

Browse files
committed
Added wave-clip virtual property
1 parent 324e9d8 commit e487108

File tree

8 files changed

+146
-14
lines changed

8 files changed

+146
-14
lines changed

dist/assembler.cjs.js

+33-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assembler.es.js

+33-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assembler.js

+33-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assembler.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@asmcss/assembler",
3-
"version": "0.8.1",
3+
"version": "0.8.2",
44
"main": "dist/assembler.cjs.js",
55
"module": "dist/assembler.es.js",
66
"browser": "dist/assembler.js",

src/StyleHandler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export default class StyleHandler {
172172
value = DEFAULT_VALUES[original] || '';
173173
}
174174
if (VALUE_WRAPPER.hasOwnProperty(original)) {
175-
value = VALUE_WRAPPER[original](value, original, media, state);
175+
value = VALUE_WRAPPER[original](value, original, media, state, this);
176176
}
177177
if (!Array.isArray(value)) {
178178
value = Array(properties.length).fill(value.replace(VAR_REGEX, "var(--$1)"));

src/list.ts

+40-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
import type StyleHandler from "./StyleHandler";
18+
1719
const positive_number_regex = /^[0-9]+(\.5)?$/;
1820
const number_regex = /^-?[0-9]+(\.5)?$/;
1921
const font_size_regex = /^(xs|sm|base|lg|([2-9])?xl)$/;
@@ -24,6 +26,10 @@ const letter_spacing_regex = /^(tighter|tight|normal|wide|wider|widest)$/;
2426
const radius_regex = /^(xs|sm|md|lg|xl|pill)$/;
2527
const order_regex = /^(first|last|none)$/;
2628

29+
// do not match comma inside parenthesis
30+
// 2px, linear-gradient(blue, red), inline => [2px, linear-gradient(blue, red), inline]
31+
const args_delimiter = /\s*,\s*(?![^(]*\))/gm;
32+
2733
export const PROPERTY_LIST = [
2834
"align-content",
2935
"align-items",
@@ -353,6 +359,7 @@ export const ALIASES = {
353359
"capitalize": "text-transform",
354360
"normal-case": "text-transform",
355361
"variant": "font-variant-numeric",
362+
"wave-clip": "clip-path",
356363
};
357364
export const DEFAULT_VALUES = {
358365
"border": ["1px solid transparent"],
@@ -383,7 +390,8 @@ export const DEFAULT_VALUES = {
383390
"capitalize": "capitalize",
384391
"normal-case": "none",
385392
"radius": "sm",
386-
"shadow": "1"
393+
"shadow": "1",
394+
"wave-clip": "50, 2, 50"
387395
}
388396

389397
const unit = v => number_regex.test(v) ? `calc(${v} * @unit-size)` : v;
@@ -415,6 +423,35 @@ const orderCallback = v => {
415423
return "0";
416424
}
417425

426+
const waveClipIds = new Set<string>();
427+
const generateWave = (value: string, original: string, media: string, state: string, handler: StyleHandler) => {
428+
const args = value.split(args_delimiter).map(v => v.trim()).map(v => parseInt(v));
429+
let [amplitude, frequency, precision] = args;
430+
431+
amplitude = amplitude ?? 50;
432+
frequency = frequency ?? 2;
433+
precision = precision ?? 50;
434+
435+
const id = amplitude + '-' + frequency + '-' + precision;
436+
if (waveClipIds.has(id)) {
437+
return '@wave-clip-' + id;
438+
}
439+
440+
waveClipIds.add(id);
441+
const units = Math.PI * 2 * frequency;
442+
const factor = precision / 100;
443+
amplitude /= 2;
444+
445+
let polygon = 'polygon(100% 0%, 0% 0%';
446+
for (let i = 0; i <= precision; i++) {
447+
const val = Math.abs((amplitude * Math.cos((i / precision) * units) - amplitude)).toFixed(2);
448+
polygon += ', ' + (i / factor) + '% calc(100% - ' + val + 'px)';
449+
}
450+
polygon += ')';
451+
(handler.style.cssRules[0] as CSSStyleRule).style.setProperty('--wave-clip-' + id, polygon);
452+
return '@wave-clip-' + id;
453+
}
454+
418455
export const VALUE_WRAPPER = {
419456
"img": v => `url(${v})`,
420457
"gradient": (value) => `linear-gradient(${value})`,
@@ -481,5 +518,6 @@ export const VALUE_WRAPPER = {
481518
"min-width": positive_unit,
482519
"max-width": positive_unit,
483520
"min-height": positive_unit,
484-
"max-height": positive_unit
521+
"max-height": positive_unit,
522+
"wave-clip": generateWave,
485523
};

types/list.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type StyleHandler from "./StyleHandler";
12
export declare const PROPERTY_LIST: string[];
23
export declare const PROPERTY_VARIANTS: {
34
animation: string[];
@@ -129,6 +130,7 @@ export declare const ALIASES: {
129130
capitalize: string;
130131
"normal-case": string;
131132
variant: string;
133+
"wave-clip": string;
132134
};
133135
export declare const DEFAULT_VALUES: {
134136
border: string[];
@@ -160,6 +162,7 @@ export declare const DEFAULT_VALUES: {
160162
"normal-case": string;
161163
radius: string;
162164
shadow: string;
165+
"wave-clip": string;
163166
};
164167
export declare const VALUE_WRAPPER: {
165168
img: (v: any) => string;
@@ -228,4 +231,5 @@ export declare const VALUE_WRAPPER: {
228231
"max-width": (v: any) => any;
229232
"min-height": (v: any) => any;
230233
"max-height": (v: any) => any;
234+
"wave-clip": (value: string, original: string, media: string, state: string, handler: StyleHandler) => string;
231235
};

0 commit comments

Comments
 (0)