Skip to content

Refactor components with modifiers #65

Description

@luca-rath

Components which toggle modifier classes on some elements should be refactored to make them also work, if the element doesn't have a class. The following code snippet does exactly that. Maybe this can be extracted into a utils.js file

appendModifier = (className: string | undefined, modifier: string): string => {
    if (!className) {
        return modifier;
    }

    return className + '--' + modifier;
};

getFirstClass = (element: HTMLElement): string | undefined => {
    return element.classList[0];
};

It can the be used like this

const containerOpenClass = this.appendModifier(this.getFirstClass(this.container), 'open');
this.container.classList.toggle(containerOpenClass);

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions