AgnosticStyles is a lightweight utility that safely applies CSS styles or class changes to a DOM element if it exists. It allows for flexible manipulation of styles and classes while ensuring the element is present, helping avoid runtime errors. Additionally, it supports optional logging and warnings through customizable debug flags.
To install AgnosticStyles using npm, run the following command:
npm install agnostic-styles
To use AgnosticStyles, import the agnosticStyles
function and configure it with the desired debug options.
import { agnosticStyles } from 'agnostic-styles';
window.agnosticStyles = agnosticStyles({ debugLog: true, debugWarn: false });
window.safeClassAdd = window.agnosticStyles('addClass', 'rounded-full');
safeClassAdd('element-id'); // Adds the 'rounded-full' class to the element with ID 'element-id'
-or-
window.safeClassAdd = window.agnosticStyles('addClass', ['someClass', 'anotherClass', 'moreClasses']);
safeClassAdd('element-id'); // Adds 'someClass', 'anotherClass', and 'moreClasses' classes to the element with ID 'element-id'
window.safeClassRemove = window.agnosticStyles('removeClass', 'rounded-full');
safeClassRemove('element-id'); // Removes the 'rounded-full' class from the element with ID 'element-id'
-or-
window.safeClassAdd = window.agnosticStyles('removeClass', ['someClass', 'anotherClass', 'moreClasses']);
safeClassAdd('element-id'); // Removes 'someClass', 'anotherClass', and 'moreClasses' classes from the element with ID 'element-id'
window.safeStyleChange = window.agnosticStyles('setStyle', { backgroundColor: 'red' });
safeStyleChange('element-id'); // Sets the background color to red on the element with ID 'element-id'
-or-
window.safeStyleChange = window.agnosticStyles('setStyle', { borderRadius: '50px' });
safeStyleChange('element-id'); // Sets border-radius to 50px on the element with ID 'element-id'
window.safeStyleRemove = window.agnosticStyles('removeStyle', ['background-color']);
safeStyleRemove('element-id'); // Removes the background color style from the element with ID 'element-id'
-or-
window.safeStyleRemove = window.agnosticStyles('removeStyle', ['border-radius']);
safeStyleRemove('element-id'); // Removes the border-radius style from the element with ID 'element-id'
You can enable or disable debugging logs and warnings using the following flags:
debugLog
: When set totrue
, it logs informational messages to the console (console.log
).debugWarn
: When set totrue
, it shows warning messages in the console (console.warn
).
By default, both options are disabled.
Show only debug logs:
window.agnosticStyles = agnosticStyles({ debugLog: true, debugWarn: false });
Show only warnings:
window.agnosticStyles = agnosticStyles({ debugLog: false, debugWarn: true });
Disable all logs and warnings:
window.agnosticStyles = agnosticStyles;
-or-
window.agnosticStyles = agnosticStyles({ debugLog: false, debugWarn: false });
Adds one or more classes to the specified element.
agnosticStyles('addClass', 'class-name');
agnosticStyles('addClass', ['class-one', 'class-two']);
Removes one or more classes from the specified element.
agnosticStyles('removeClass', 'class-name');
agnosticStyles('removeClass', ['class-one', 'class-two']);
Sets one or more inline CSS styles on the specified element.
agnosticStyles('setStyle', { backgroundColor: 'blue', color: 'white' });
Removes one or more inline CSS styles from the specified element.
agnosticStyles('removeStyle', ['background-color', 'color']);
agnosticStyles('removeStyle', ['border-radius']);
- Manipulate non-existent elements: If the element doesn't exist, the action will be skipped, and a warning may be shown depending on debug options.
- Unsupported actions: If an unsupported action is passed, AgnosticStyles will log a warning, and the operation will not proceed.
AgnosticStyles is licensed under the MIT License. See the LICENSE file for more details.
If you encounter any issues, feel free to report them here.