From dde22dea4259d61dc228b979f9ed5ac484898e46 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Fri, 10 May 2024 15:20:32 +0300 Subject: [PATCH] chore(tailwind): allow scale 0 in plugin --- plugins-demo/tailwind-demo/src/index.html | 10 ++++- plugins-demo/tailwind-demo/tailwind.config.js | 8 ++++ plugins/tailwind/src/preparsed.ts | 41 +++++++++++-------- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/plugins-demo/tailwind-demo/src/index.html b/plugins-demo/tailwind-demo/src/index.html index ec203ff..534cdfa 100644 --- a/plugins-demo/tailwind-demo/src/index.html +++ b/plugins-demo/tailwind-demo/src/index.html @@ -32,6 +32,12 @@ >

+

+ Using width/height to resize icon: + +

Dynamic selectors plugin, custom options

@@ -98,7 +104,9 @@

Custom size set via width/height: - +

diff --git a/plugins-demo/tailwind-demo/tailwind.config.js b/plugins-demo/tailwind-demo/tailwind.config.js index 296587a..40dbbff 100644 --- a/plugins-demo/tailwind-demo/tailwind.config.js +++ b/plugins-demo/tailwind-demo/tailwind.config.js @@ -84,6 +84,14 @@ module.exports = { }, ], }), + // Main plugin, no size + addIconSelectors({ + maskSelector: '.iconify-nosize', + backgroundSelector: '', + scale: 0, + // Empty prefixes list: reusing data from plugin above + prefixes: [], + }), // Plugin with clean selectors: requires writing all used icons in first parameter addCleanIconSelectors(['mdi-light:home']), // Plugin with dynamic selectors diff --git a/plugins/tailwind/src/preparsed.ts b/plugins/tailwind/src/preparsed.ts index 8bb6f0c..1027a45 100644 --- a/plugins/tailwind/src/preparsed.ts +++ b/plugins/tailwind/src/preparsed.ts @@ -29,12 +29,19 @@ export function getCSSRulesForPlugin(options: IconifyPluginOptions) { const varName = fullOptions.varName || 'svg'; // Scale icons - const overrideRules: Record = fullOptions.scale - ? { - width: fullOptions.scale + 'em', - height: fullOptions.scale + 'em', - } - : {}; + const scale = fullOptions.scale ?? 1; + const adjustScale = (obj: Record) => { + if (!scale) { + // Delete width and height + delete obj['width']; + delete obj['height']; + } else if (scale !== 1) { + // Set custom width and height + obj['width'] = scale + 'em'; + obj['height'] = scale + 'em'; + } + return obj; + }; // Add common rules const maskSelector = fullOptions.maskSelector ?? '.iconify'; @@ -42,21 +49,23 @@ export function getCSSRulesForPlugin(options: IconifyPluginOptions) { fullOptions.backgroundSelector ?? '.iconify-color'; if (maskSelector) { rules[maskSelector] = Object.assign( - getCommonCSSRules({ - mode: 'mask', - varName, - }), - overrideRules, + adjustScale( + getCommonCSSRules({ + mode: 'mask', + varName, + }) + ), fullOptions.extraMaskRules || {} ); } if (backgroundSelector) { rules[backgroundSelector] = Object.assign( - getCommonCSSRules({ - mode: 'background', - varName, - }), - overrideRules, + adjustScale( + getCommonCSSRules({ + mode: 'background', + varName, + }) + ), fullOptions.extraBackgroundRules || {} ); }