2023-01-11 17:42:21 +02:00
|
|
|
import plugin from 'tailwindcss/plugin';
|
2023-01-12 20:02:54 +02:00
|
|
|
import { getCSSRulesForIcons } from './clean';
|
|
|
|
import { getDynamicCSSRules } from './dynamic';
|
|
|
|
import type {
|
|
|
|
CleanIconifyPluginOptions,
|
|
|
|
DynamicIconifyPluginOptions,
|
2024-05-06 21:23:05 +03:00
|
|
|
IconifyPluginOptions,
|
|
|
|
IconifyPluginOptionsObject,
|
|
|
|
} from './helpers/options';
|
|
|
|
import { getCommonCSSRules } from '@iconify/utils/lib/css/common';
|
2023-01-11 17:42:21 +02:00
|
|
|
|
|
|
|
/**
|
2023-01-12 20:02:54 +02:00
|
|
|
* Generate styles for dynamic selector: class="icon-[mdi-light--home]"
|
2023-01-11 17:42:21 +02:00
|
|
|
*/
|
2023-01-12 20:02:54 +02:00
|
|
|
export function addDynamicIconSelectors(options?: DynamicIconifyPluginOptions) {
|
|
|
|
const prefix = options?.prefix || 'icon';
|
|
|
|
return plugin(({ matchComponents }) => {
|
|
|
|
matchComponents({
|
2024-04-28 19:12:49 +03:00
|
|
|
[prefix]: (icon: string) => {
|
|
|
|
try {
|
|
|
|
return getDynamicCSSRules(icon, options);
|
|
|
|
} catch (err) {
|
|
|
|
// Log error, but do not throw it
|
|
|
|
console.error(err.message);
|
|
|
|
}
|
|
|
|
},
|
2023-01-12 20:02:54 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate styles for preset list of icons
|
|
|
|
*/
|
|
|
|
export function addCleanIconSelectors(
|
2023-02-07 21:34:26 +02:00
|
|
|
icons: string[] | string,
|
2023-01-12 20:02:54 +02:00
|
|
|
options?: CleanIconifyPluginOptions
|
2023-01-11 17:42:21 +02:00
|
|
|
) {
|
2023-02-07 21:34:26 +02:00
|
|
|
const rules = getCSSRulesForIcons(icons, options);
|
|
|
|
return plugin(({ addUtilities }) => {
|
2023-01-12 20:02:54 +02:00
|
|
|
addUtilities(rules);
|
2023-01-11 17:42:21 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-06 21:23:05 +03:00
|
|
|
/**
|
|
|
|
* Iconify plugin
|
|
|
|
*
|
|
|
|
* TODO: export it when ready
|
|
|
|
*/
|
|
|
|
function iconifyPlugin(options: IconifyPluginOptions) {
|
|
|
|
return plugin(({ addUtilities }) => {
|
|
|
|
const rules = Object.create(null) as Record<
|
|
|
|
string,
|
|
|
|
Record<string, string>
|
|
|
|
>;
|
|
|
|
|
|
|
|
// Convert options to object
|
|
|
|
const fullOptions: IconifyPluginOptionsObject = Array.isArray(options)
|
|
|
|
? {
|
|
|
|
prefixes: options,
|
|
|
|
}
|
|
|
|
: options;
|
|
|
|
|
|
|
|
// Variable name, default to 'svg' (cannot be empty string)
|
|
|
|
const varName = fullOptions.varName || 'svg';
|
|
|
|
|
|
|
|
// Add common rules
|
|
|
|
const mask = fullOptions.mask ?? '.iconify';
|
|
|
|
const background = fullOptions.background ?? '.iconify-color';
|
|
|
|
if (mask) {
|
|
|
|
rules[mask] = getCommonCSSRules({
|
|
|
|
mode: 'mask',
|
|
|
|
varName,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (background) {
|
|
|
|
rules[background] = getCommonCSSRules({
|
|
|
|
mode: 'background',
|
|
|
|
varName,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
addUtilities(rules);
|
|
|
|
|
|
|
|
// TODO: add icon sets
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-11 17:42:21 +02:00
|
|
|
/**
|
2023-01-12 20:02:54 +02:00
|
|
|
* Export types
|
2023-01-11 17:42:21 +02:00
|
|
|
*/
|
2024-05-06 21:23:05 +03:00
|
|
|
export type {
|
|
|
|
CleanIconifyPluginOptions,
|
|
|
|
DynamicIconifyPluginOptions,
|
|
|
|
IconifyPluginOptions,
|
|
|
|
};
|