Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggested API for Bliss 2.0 - attributes and class #246

Closed
cyfung1031 opened this issue Aug 28, 2021 · 0 comments
Closed

Suggested API for Bliss 2.0 - attributes and class #246

cyfung1031 opened this issue Aug 28, 2021 · 0 comments

Comments

@cyfung1031
Copy link

In the current version, it is hard to add/remove attribute and className using Bliss's functions

one common use in jQuery is

$('a.selected').removeClass('selected');
$('button.btn[disabled]').removeAttr('disabled').attr('custom-data','123');

In the current bliss,

$$('a.selected').forEach(a=>a.classList.remove('selected'));
$$('button.btn[disabled]')._.removeAttribute('disabled')._.setAttribute('custom-data','123');

If you do that operation with other setProps, it would be so ugly.

$$('a.selected')._.attributes({
    href: "http://google.com"
}).forEach(a => a.classList.remove('selected'));


$$('button.btn[disabled]')._.attributes({
    'custom-data': '123'
})._.removeAttribute('disabled')

I would suggest to have two APIs for that purpose.

$.setProps.sAttr = function(o) {
    for (var attribute in o) {
        let v = o[attribute]
        switch (v) {
            case null:
                break;
            case false:
                this.removeAttribute(attribute);
                break;
            case true:
                v = "";
            default:
                this.setAttribute(attribute, v);
        }
    }
};
$.setProps.sClass = function(o) {
    for (var c in o) {
        switch (o[c]) {
            case true:
                this.classList.add(c);
                break;
            case false:
                this.classList.remove(c);
                break;
        }
    }
};
$.add({
    'sAttr': $.setProps.sAttr,
    'sClass': $.setProps.sClass
});

then the code become much better.

$$('a.selected')._.set({
    sClass: {
        selected: false
    },
    attributes: {
        href: "http://google.com"
    }
});
$$('button.btn[disabled]')._.set({
    sAttr: {
        'custom-data': '123',
        'disabled': false
    }
})

Remarks:

  1. .attributes() can be merged with .sAttr()
  2. .toggleAttribute() can be removed as .sClass() is more powerful and consistence with your setProps design.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant