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,
|
|
|
|
} from './options';
|
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({
|
|
|
|
[prefix]: (icon: string) => getDynamicCSSRules(icon, options),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate styles for preset list of icons
|
|
|
|
*/
|
|
|
|
export function addCleanIconSelectors(
|
2023-01-11 23:42:32 +02:00
|
|
|
icons?: string[] | string,
|
2023-01-12 20:02:54 +02:00
|
|
|
options?: CleanIconifyPluginOptions
|
2023-01-11 17:42:21 +02:00
|
|
|
) {
|
2023-01-11 23:42:32 +02:00
|
|
|
const passedOptions =
|
|
|
|
typeof icons === 'object' && !(icons instanceof Array)
|
|
|
|
? icons
|
|
|
|
: options || {};
|
|
|
|
const passedIcons =
|
|
|
|
typeof icons !== 'object' || icons instanceof Array ? icons : void 0;
|
|
|
|
|
|
|
|
// Get hardcoded list of icons
|
|
|
|
const rules = passedIcons
|
2023-01-12 20:02:54 +02:00
|
|
|
? getCSSRulesForIcons(passedIcons, passedOptions)
|
2023-01-11 23:42:32 +02:00
|
|
|
: void 0;
|
|
|
|
|
|
|
|
return plugin(({ addUtilities, matchComponents }) => {
|
2023-01-12 20:02:54 +02:00
|
|
|
addUtilities(rules);
|
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
|
|
|
*/
|
2023-01-12 20:02:54 +02:00
|
|
|
export type { CleanIconifyPluginOptions, DynamicIconifyPluginOptions };
|