Skip to content

Commit

Permalink
Add scale option to tailwind plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
IHIutch committed Nov 24, 2023
1 parent 72aa8aa commit bce4af6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
10 changes: 9 additions & 1 deletion plugins/tailwind/src/dynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { DynamicIconifyPluginOptions } from './options';
*/
export function getDynamicCSSRules(
icon: string,
options: DynamicIconifyPluginOptions = {}
{ scale = 1, ...options }: DynamicIconifyPluginOptions = {}
): Record<string, string> {
const nameParts = icon.split(/--|\:/);
if (nameParts.length !== 2) {
Expand All @@ -34,6 +34,14 @@ export function getDynamicCSSRules(
throw new Error(`Cannot find "${icon}". Bad icon name?`);
}

if (scale) {
generated.common.rules.height = scale + 'em'
generated.common.rules.width = scale + 'em'
} else {
delete generated.common.rules.height
delete generated.common.rules.width
}

return {
// Common rules
...(options.overrideOnly || !generated.common?.rules
Expand Down
3 changes: 3 additions & 0 deletions plugins/tailwind/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ export interface DynamicIconifyPluginOptions

// Include icon-specific selectors only
overrideOnly?: true;

// Scale relative to the the default value, (ex. scale: 2 = 2em). Set to 0 to disable default value.
scale?: number
}
36 changes: 36 additions & 0 deletions plugins/tailwind/tests/dynamic-css-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,42 @@ describe('Testing dynamic CSS rules', () => {
});
});

it('Scale appropriate sets height/width default value', () => {
const data = getDynamicCSSRules('mdi-light--home', {
scale: 2,
});
expect(data).toEqual({
'display': 'inline-block',
'width': '2em',
'height': '2em',
'background-color': 'currentColor',
'-webkit-mask-image': 'var(--svg)',
'mask-image': 'var(--svg)',
'-webkit-mask-repeat': 'no-repeat',
'mask-repeat': 'no-repeat',
'-webkit-mask-size': '100% 100%',
'mask-size': '100% 100%',
'--svg': data['--svg'],
});
});

it('Scale removes default value', () => {
const data = getDynamicCSSRules('mdi-light--home', {
scale: 0,
});
expect(data).toEqual({
'display': 'inline-block',
'background-color': 'currentColor',
'-webkit-mask-image': 'var(--svg)',
'mask-image': 'var(--svg)',
'-webkit-mask-repeat': 'no-repeat',
'mask-repeat': 'no-repeat',
'-webkit-mask-size': '100% 100%',
'mask-size': '100% 100%',
'--svg': data['--svg'],
});
});

it('Missing icon', () => {
let threw = false;
try {
Expand Down

0 comments on commit bce4af6

Please sign in to comment.