UNPKG

@medyll/idae-be

Version:

A modern, lightweight, and extensible DOM manipulation library built with TypeScript. Designed for precise element targeting and manipulation using a callback-based approach. Features include advanced DOM traversal, event handling, style management, attri

64 lines (63 loc) 2.08 kB
import { createBe } from '../be.js'; // Définition des presets avec des types constants const examplePresets = { display: ['flex', 'grid', 'string'], position: ['absolute', 'fixed', 'static', 'relative'], margin: ['top', 'bottom', 'left', 'right'] }; export class DynamicHandler { beElement; attr; constructor(element, attr = 'style') { this.beElement = element; this.attr = attr; } handler = { get: (target, prop) => { if (prop in target) { return Reflect.get(target, prop); } const matchSet = prop.match(/^set([A-Z]\w*)([A-Z]\w*)?$/); if (matchSet) { const [, mainProp, subProp] = matchSet; const cssProp = mainProp.toLowerCase(); if (subProp) { const cssValue = subProp.toLowerCase(); return () => { this.beElement.eachNode((el) => { el[this.attr][cssProp] = cssValue; }); }; } else { return (value) => { this.beElement.eachNode((el) => { if (value === undefined) { el[this.attr][cssProp] = ''; } else { el[this.attr][cssProp] = value; } }); }; } } return undefined; } }; proxy() { return new Proxy(this, this.handler); } } // Utilisation /* const div = createBe('div'); const styler = new DynamicHandler(div, 'style').proxy(); */ // Utilisation des méthodes dynamiques /* styler.setDisplay('block'); styler.setDisplay(); styler.setDisplayNone(); styler.setPosition('relative'); styler.setPositionAbsolute(); styler.setColor('red'); styler.setBackgroundColor('#f0f0f0'); styler.setFontSize('16px'); */