UNPKG

arto

Version:

Arto is a flexible and type-safe class name management library for building scalable UIs with variants, states, and conditional styling.

107 lines 4.83 kB
import { Plugin, PluginStage } from '../plugin-interface'; import { ClassNameBuilder } from '../classname-builder'; import { LogicOperator, ArtoLogicObject } from '../types/rules'; import { VariantValue } from '../types'; /** * Evaluates an array of boolean conditions with a single logical operator. * Supported operators: * - **'AND'**: All conditions must be `true`. * - **'OR'**: At least one condition is `true`. * - **'NOT'**: All conditions must be `false`. * - **'XOR'**: Exactly one condition is `true`. * - **'IMPLIES'**: For two conditions, `!A || B`. If more than two are present, only the first two are considered. * * @param conditions - The array of boolean values to evaluate. * @param op - The logical operator to apply; defaults to `'AND'` if none is provided. * @returns `true` if conditions pass under the specified operator, otherwise `false`. * * @example * ```ts * evaluateSimpleLogic([true, false], 'AND') // => false * evaluateSimpleLogic([true, false], 'OR') // => true * ``` */ export declare function evaluateSimpleLogic(conditions: boolean[], op: LogicOperator | undefined): boolean; /** * Separately evaluates arrays of boolean values for variants and states, then * combines them using a final operator (`combine`). * * @param variantBooleans - Boolean results for each variant match. * @param stateBooleans - Boolean results for each state match. * @param logicObj - Defines how to evaluate 'variants', 'states', and how to 'combine' them. * @returns `true` if both variant and state logic pass under the specified combination operator, otherwise `false`. * * @example * ```ts * evaluateObjectLogic([true, true], [false, true], { * variants: 'AND', // must all be true * states: 'OR', // at least one true * combine: 'AND' // both results must be true * }) * // => true && true => true * ``` */ export declare function evaluateObjectLogic(variantBooleans: boolean[], stateBooleans: boolean[], logicObj: ArtoLogicObject): boolean; /** * A plugin that handles advanced conditional logic (rules) in `artoConfig.rules`. * Each rule can remove or add classes when specific variant/state conditions pass. * * **Key Steps**: * 1) For each rule, check if the `when` conditions pass (using variants/states/logic). * 2) If so, remove specified classes (variants/states/base) and add new classes if needed. * * @template TVariants - A record of variant keys & possible values. * @template TStates - A string union of valid state names. * @template TContext - Optional context type for rule callbacks. */ export declare class RulesPlugin<TVariants extends Record<string, VariantValue>, TStates extends string, TContext = unknown> implements Plugin<TVariants, TStates, TContext> { /** * A unique ID for this plugin. Used for debugging or HMR consistency. */ id: string; /** * Runs at the 'core' stage by default. Typically assigned a higher `order` * so that state and variant classes are already applied before rules run. */ stage: PluginStage; /** * Plugin priority within the 'core' stage (default = 0). * Often set to a higher value (e.g., 3) in `arto.ts`. */ order: number; /** * @param order - Numeric priority in the 'core' stage. */ constructor(order?: number); /** * Main entry point called by the builder. Iterates over each rule in `artoConfig.rules`, * checks whether the rule conditions pass, and if so: * 1) Removes classes (per `rule.remove`). * 2) Adds classes (per `rule.add`). * * @param builder - The builder that manages class buckets for variants, states, etc. */ apply(builder: ClassNameBuilder<TVariants, TStates, TContext>): void; /** * Evaluates whether a rule's `when` conditions are met using variants, states, * and optional logic operations. * * @param when - Specifies which variants and states must match, plus an optional logic definition. * @param selectedVariants - The user's chosen variant values. * @param activeStates - The set of active states. * @param context - Optional context for custom logic callbacks. * @returns `true` if the rule should apply, otherwise `false`. */ private doesRuleApply; /** * Removes classes from the builder according to the `ArtoRuleRemove` settings: * - Clears variant classes for specified variant keys. * - Clears state classes for specified states, possibly with a `statesScope`. * - Clears all base classes if `remove.base` is true. * * @param builder - The ClassNameBuilder managing class buckets. * @param remove - The removal instructions (variant keys, states, base). */ private removeStuff; } //# sourceMappingURL=rules-plugin.d.ts.map