2023-01-11 15:42:21 +00:00
|
|
|
import plugin from 'tailwindcss/plugin';
|
2023-01-12 18:02:54 +00:00
|
|
|
import { getCSSRulesForIcons } from './clean';
|
|
|
|
import { getDynamicCSSRules } from './dynamic';
|
|
|
|
import type {
|
|
|
|
CleanIconifyPluginOptions,
|
|
|
|
DynamicIconifyPluginOptions,
|
2024-05-06 18:23:05 +00:00
|
|
|
IconifyPluginOptions,
|
|
|
|
} from './helpers/options';
|
2024-05-13 16:53:45 +00:00
|
|
|
import {
|
|
|
|
cleanupIconifyPluginOptions,
|
|
|
|
getCSSComponentsForPlugin,
|
|
|
|
getCSSRulesForPlugin,
|
|
|
|
} from './preparsed';
|
2023-01-11 15:42:21 +00:00
|
|
|
|
|
|
|
/**
|
2024-05-10 07:00:08 +00:00
|
|
|
* Generate styles for dynamic selector
|
|
|
|
*
|
|
|
|
* Usage in HTML: <span class="icon-[mdi-light--home]" />
|
2023-01-11 15:42:21 +00:00
|
|
|
*/
|
2023-01-12 18:02:54 +00:00
|
|
|
export function addDynamicIconSelectors(options?: DynamicIconifyPluginOptions) {
|
|
|
|
const prefix = options?.prefix || 'icon';
|
|
|
|
return plugin(({ matchComponents }) => {
|
|
|
|
matchComponents({
|
2024-04-28 16:12:49 +00: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 18:02:54 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-05-10 07:00:08 +00:00
|
|
|
* Generate rules for mask, background and selected icon sets
|
|
|
|
*
|
|
|
|
* Icons should combine either mask or background selector and icon selector
|
|
|
|
*
|
|
|
|
* This plugin generates only square icons. Icons that are not square will be resized to fit square.
|
|
|
|
*
|
|
|
|
* Usage in HTML: <span class="iconify mdi-light--home" />
|
2023-01-12 18:02:54 +00:00
|
|
|
*/
|
2024-05-10 07:00:08 +00:00
|
|
|
export function addIconSelectors(options: IconifyPluginOptions) {
|
2024-05-13 16:53:45 +00:00
|
|
|
const fullOptions = cleanupIconifyPluginOptions(options);
|
|
|
|
|
2024-05-13 15:08:45 +00:00
|
|
|
return plugin(({ addComponents, addUtilities }) => {
|
2024-05-13 16:53:45 +00:00
|
|
|
addComponents(getCSSComponentsForPlugin(fullOptions));
|
|
|
|
addUtilities(getCSSRulesForPlugin(fullOptions));
|
2023-01-11 15:42:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-06 18:23:05 +00:00
|
|
|
/**
|
2024-05-10 07:00:08 +00:00
|
|
|
* Generate styles for preset list of icons
|
2024-05-06 18:23:05 +00:00
|
|
|
*
|
2024-05-10 07:00:08 +00:00
|
|
|
* Requires knowing full list of icons
|
|
|
|
*
|
|
|
|
* Usage in HTML: <span class="icon--mdi-light icon--mdi-light--home" />
|
|
|
|
*
|
|
|
|
* @deprecated Use addIconSelectors instead
|
2024-05-06 18:23:05 +00:00
|
|
|
*/
|
2024-05-10 07:00:08 +00:00
|
|
|
export function addCleanIconSelectors(
|
|
|
|
icons: string[] | string,
|
|
|
|
options?: CleanIconifyPluginOptions
|
|
|
|
) {
|
|
|
|
const rules = getCSSRulesForIcons(icons, options);
|
2024-05-06 18:23:05 +00:00
|
|
|
return plugin(({ addUtilities }) => {
|
|
|
|
addUtilities(rules);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-11 15:42:21 +00:00
|
|
|
/**
|
2023-01-12 18:02:54 +00:00
|
|
|
* Export types
|
2023-01-11 15:42:21 +00:00
|
|
|
*/
|
2024-05-06 18:23:05 +00:00
|
|
|
export type {
|
|
|
|
CleanIconifyPluginOptions,
|
|
|
|
DynamicIconifyPluginOptions,
|
|
|
|
IconifyPluginOptions,
|
|
|
|
};
|