@nkeff/cardboard-js
    Preparing search index...

    Class CTag

    This is the main class in Cardboard. Even though Cardboard is designed to not need to use this class directly, you can if you want.

    CTag contains a reference to an HTMLElement, its parent, and provides a set of methods to interact with it.

    Index

    Constructors

    Properties

    el: HTMLElement & { remove: () => any }

    Reference to the HTMLElement that this

    Accessors

    • get checked(): any

      Gets the checked state of the HTMLElement that this CTag represents, if it is a checkbox or radio button.

      Returns any

    • get children(): Node[]

      Returns Node[]

    • get classList(): DOMTokenList

      Gets the classlist of the HTMLElement that this CTag represents.

      Returns DOMTokenList

    • get className(): string

      Gets the classname of the HTMLElement that this CTag represents.

      Returns string

    • get consumeValue(): any

      Gets the value of the element and clears the value

      Returns any

    • get id(): string

      Get's the id of the HTMLElement that this CTag represents.

      Returns string

    • get style(): CSSStyleDeclaration

      Gets the style of the HTMLElement that this CTag represents.

      Returns CSSStyleDeclaration

    • get value(): any

      Gets the value of the HTMLElement that this CTag represents, if it has a value.

      Returns any

    • get visible(): boolean

      Returns boolean

    • set visible(newValue: boolean): void

      Parameters

      • newValue: boolean

      Returns void

    Methods

    • Add classes to the elements class list.

      Parameters

      • ...classes: string[]

        The classes to add to the element's class list.

      Returns CTag

      • The current CTag instance, allowing for method chaining
      const tag = new CTag('div');
      tag.addClass('class1', 'class2');
    • Appends the given children to the element.

      Parameters

      • ...children: TagChildren

        The children to append to the element.

      Returns CTag

      • The current CTag instance, allowing for method chaining.
      const tag = new CTag('div');
      tag.append(
      new CTag('span', ['Child 1']),
      new CTag('span', ['Child 2']),
      );
    • Add attribute to the element when the consumer is truthy. Updates whenever the observable changes. value can be a string or a function that returns a string. If invert is set to true, the condition will be inversed, but you can also use attrIfNot

      Type Parameters

      • T

      Parameters

      Returns CTag

    • Add attribute to the element when the consumer is falsy. Updates whenever the observable changes. value can be a string or a function that returns a string. If invert is set to true, the condition will be inversed

      Type Parameters

      • T

      Parameters

      Returns CTag

    • Add a change event listener

      Parameters

      • fn: (tag: CTag, evt: Event) => void

      Returns CTag

    • Adds classes to the element when the observable is truthy, and removes them when it is falsy. Updates whenever the observable changes. You can pass in an array of classes, or a function that returns a list of classes. If invert is set to true, the condition will be inversed, but you can also use classIfNot

      Type Parameters

      • T

      Parameters

      • observable: IObservable<T>

        The observable to listen to.

      • classes: string[] | ((self: CTag) => string[])

        The classes to add to the element. Can be an array of strings or a function that returns an array of strings.

      • Optionalinvert: boolean = false

        If true, the condition will be inversed.

      Returns CTag

      • The current CTag instance, allowing for method chaining.
      const isActive = createObservable(true);
      const tag = new CTag('div');

      // Adds 'active' and 'highlighted' classes when isActive is true
      tag.classIf(isActive, ['active', 'highlighted']);
    • Adds classes to the element when the observable is falsy, and removes them when it is truthy. Updates whenever the observable changes. You can pass in an array of classes, or a function that returns a list of classes.

      Type Parameters

      • T

      Parameters

      • observable: IObservable<T>

        The observable to listen to.

      • classes: string[] | ((self: CTag) => string[])

        The classes to add to the element. Can be an array of strings or a function that returns an array of strings.

      Returns CTag

      • The current CTag instance, allowing for method chaining.
      const isActive = createObservable(true);
      const tag = new CTag('div');

      // Adds 'inactive' classes when isActive is false
      tag.classIfNot(isActive, ['inactive']);
    • Clears the value of the element. If you are getting the value and then clearing, consider using consumeValue

      Returns CTag

    • Add a click event listener

      Parameters

      • fn: (tag: CTag, evt: MouseEvent) => void

      Returns CTag

    • Configure the element in a single call by passing

      Parameters

      • c: TagConfig

        instead of having to call a method for each property you want to change

      Returns CTag

      • The current CTag instance, allowing for method chaining
      const tag = new CTag('div');
      tag.config({
      attr: { id: 'my-div', 'data-custom': 'value' },
      classList: ['class1', 'class2'],
      className: 'my-class',
      style: { color: 'red', backgroundColor: 'blue' },
      text: 'Hello World',
      value: 'Initial Value',
      children: [new CTag('span', ['Child Text'])],
      on: {
      click: (self, evt) => console.log('Clicked!', self),
      },
      });
    • Whenever the observable changes, it will call the callback. This is helpful to react to changes in observables and update the tag accordingly.

      You can also do it directly, although you need to keep a reference to the tag yourself.

      Type Parameters

      • T

      Parameters

      • observable: IObservable<T>

        The observable to listen to.

      • callback: (self: CTag, newValue?: T) => void

        The callback to call when the observable changes.

      Returns CTag

      • The current CTag instance, allowing for method chaining.
      const disabled = createObservable(false);
      const tag = new CTag('div');
      tag.consume(disabled, (self, isDisabled) => {
      console.log('New value:', isDisabled);
      self.setDisabled(isDisabled);
      });
    • Destroy the element, should not be used afterwards

      USE WITH CAUTION! Not intended to be used in most cases.

      Returns void

    • Disable this element when the consumer is truthy. Updates whenever the observable changes. If invert is set to true, the condition will be inversed, but you can also use disableIfNot

      Type Parameters

      • T

      Parameters

      Returns CTag

    • Disable this element when the consumer is falsy. Updates whenever the observable changes.

      Type Parameters

      • T

      Parameters

      Returns CTag

    • When the observable changes, it will call ifTrue when the observable is true. Or ifFalse when the observable is false. If invert is set to true, the condition will be inversed, but you can also use doIfNot

      Type Parameters

      • T

      Parameters

      • observable: IObservable<T>

        The observable to listen to.

      • ifTrue: (value?: T) => void

        The function to call when the observable is truey.

      • ifFalse: (value?: T) => void

        The function to call when the observable is falsey.

      • Optionalinvert: boolean = false

        If true, the condition will be inversed.

      Returns CTag

      • The current CTag instance, allowing for method chaining.
    • The oposite of doIf When the observable changes, it will call ifTrue if the observable is false. Or ifFalse if the observable is true.

      Type Parameters

      • T

      Parameters

      • observable: IObservable<T>

        The observable to listen to.

      • ifTrue: (value: T) => void

        The function to call when the observable is falsy.

      • ifFalse: (value: T) => void

        The function to call when the observable is truthy.

      Returns CTag

      • The current CTag instance, allowing for method chaining.
    • Find a child in this element (in the DOM or NOT)

      Parameters

      • predicate: (el: TagChild) => boolean

        A function that takes a TagChild and returns true if it matches the condition.

      Returns undefined | TagChild

      • Returns the first TagChild that matches the predicate, or undefined if no match is found.
    • Find a CTag child in this element (in the DOM or NOT)

      Parameters

      • predicate: (el: CTag) => boolean

        A function that takes a CTag and returns true if it matches the condition.

      Returns undefined | CTag

      • Returns the first CTag that matches the predicate, or undefined if no match is found.
    • Check if classes are present in this element

      Parameters

      • ...classes: string[]

      Returns boolean

    • Hide this element when the consumer is truthy. Updates whenever the observable changes. If invert is set to true, the condition will be inversed, but you can also use hideIfNot

      Type Parameters

      • T

      Parameters

      • observable: IObservable<T>

        The observable to listen to.

      • Optionalinvert: boolean = false

        If true, the condition will be inversed.

      Returns CTag

      • The current CTag instance, allowing for method chaining.
      const isHidden = createObservable(false);
      const tag = new CTag('div');
      tag.hideIf(isHidden); // Hides the tag when isHidden is true
    • Hide this element when the observable is falsy. Updates whenever the observable changes.

      Type Parameters

      • T

      Parameters

      Returns CTag

      • The current CTag instance, allowing for method chaining.
      const isVisible = createObservable(false);
      const tag = new CTag('div');
      tag.hideIfNot(isVisible); // Hides the tag when isVisible is false
    • Add a keypress event listener

      Parameters

      • fn: (tag: CTag, evt: KeyboardEvent) => void
      • Optionalkey: string

      Returns CTag

    • Prepends the given children to the element.

      Parameters

      • ...children: TagChildren

        The children to append to the element.

      Returns CTag

      • The current CTag instance, allowing for method chaining.
      const tag = new CTag('div');
      tag.prepend(
      new CTag('span', ['Child 1']),
      new CTag('span', ['Child 2']),
      );
    • Query a child in this element (in the DOM)

      Parameters

      • selector: any

        The CSS selector to query the child element.

      Returns undefined | CTag

      • Returns a CTag instance if the element is found, or undefined if not found.
      const childTag = parentTag.q('.child-class');
      
    • Remove element from the DOM, but keep data as is. Can then be added again. To fully remove the element use destroy

      USE WITH CAUTION! Not intended to be used in most cases.

      Returns Promise<CTag>

    • Replace a class with another

      Parameters

      • targetClass: string
      • replaceClass: string

      Returns CTag

    • Remove classes from class list

      Parameters

      • ...classes: string[]

      Returns CTag

    • Remove styles

      Parameters

      • ...styleNames: string[]

      Returns CTag

    • Adds a set of attributes to the element

      Parameters

      • attrs: Record<string, string | undefined>

      Returns CTag

    • Sets the checked state of the element, if it is a checkbox or radio button.

      Parameters

      • checked: boolean

      Returns CTag

    • Set the elements class name

      Parameters

      • className: string

      Returns CTag

    • Set whether the element should be disabled or not. It sets the disabled attribute.

      Parameters

      • disabled: boolean

      Returns CTag

    • Set's the id of the HTMLElement that this CTag represents.

      Parameters

      • id: string

      Returns CTag

    • Sets the value of the HTMLElement that this CTag represents.

      Parameters

      • OptionalnewValue: string

      Returns CTag

    • Adds a stylesheet to main style manager, and adds the className to the element. This is useful for adding styles to the element that are not inline styles. By doing this we can have just one style definition for tags that will have the same styles.

      Parameters

      • stylesheet: undefined | NestedStyleMap

        The stylesheet to add to the style manager.

      • OptionalclassName: string

        The class name to add to the element. If not provided, a random UUID will be generated.

      Returns this

      • The current CTag instance, allowing for method chaining.
    • Add style to the element when the consumer is truthy. Updates whenever the observable changes. If invert is set to true, the condition will be inversed, but you can also use styleIfNot value can be a string or a function that returns a string.

      Type Parameters

      • T

      Parameters

      • observable: IObservable<T>
      • style: string
      • value: string | ((self: CTag) => string) = ''
      • invert: boolean = false

      Returns CTag

    • Add style to the element when the consumer is falsy. Updates whenever the observable changes. value can be a string or a function that returns a string.

      Type Parameters

      • T

      Parameters

      • observable: IObservable<T>
      • style: string
      • value: string | ((self: CTag) => string) = ''

      Returns CTag

    • Add a submit event listener

      Parameters

      • fn: (tag: CTag, evt: SubmitEvent) => void

      Returns CTag

    • If {textTemplate} is provided, it sets the textContent of the element. If {textTemplate} is provided, and a state is provided. It will use the {textTemplate} as a template, that will be interpolated with the values in the state, each time the state changes. It acts like text

      If no argument is provided, it returns the textContent of the element.

      Type Parameters

      Parameters

      Returns J extends string ? CTag : string

    • Sets text when the consumer is true, and sets elseText (default='') when the consumer is false. Both text and elseText can be a string or a function that returns a string. Updates whenever the observable changes. If invert is set to true, the condition will be inversed, but you can also use textIfNot

      Type Parameters

      • T

      Parameters

      • observable: IObservable<T>

        The observable to listen to.

      • text: string | ((self: CTag) => string)

        The text to set when the observable is truthy. Can be a string or a function that returns a string.

      • OptionalelseText: string | ((self: CTag) => string) = ''

        The text to set when the observable is falsy. Can be a string or a function that returns a string. Defaults to an empty string.

      • Optionalinvert: boolean = false

        If true, the condition will be inversed.

      Returns CTag

      • The current CTag instance, allowing for method chaining.
    • Sets text when the consumer is falsy, and sets elseText (default='') when the consumer is truthy. Both text and elseText can be a string or a function that returns a string. Updates whenever the observable changes.

      Type Parameters

      • T

      Parameters

      • observable: IObservable<T>
      • text: string | ((self: CTag) => string)
      • elseText: string | ((self: CTag) => string) = ''

      Returns CTag

    • Toggle a class. If it's present it's removed, if it's not present its added.

      Parameters

      • targetClass: string

      Returns CTag