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

Add support for object-based styling to improve readability #379

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

zakarialaoui10
Copy link

Tip

Refactored inline styles to allow usage of JavaScript objects for improved readability and maintainability

Example :

import van from "vanjs-core";

const { a, div, li, p, ul } = van.tags;

const Hello = () =>
  div(
    p({
        style: {
          backgroundColor: "coral",
          padding: "5px",
        }
      }, "👋Hello"),
    ul(li("🗺️World"), li(a({ href: "https://vanjs.org/" }, "🍦VanJS"))),
  );

van.add(document.body, Hello());

Note

Standard inline style as a string is still supported to avoid breaking changes or side effects on existing projects

Tip

This feature could be the key to implementing conditional styling based on state changes or user interactions.

@Tao-VanJS
Copy link
Member

Thanks for the contribution!

However, object-based style doesn't support states and state bindings. For instance:

const Hello = () => {
  const color = van.state("coral")
  return div(
    p({
        style: {
          backgroundColor: color, // not reactive to `color`
          padding: "5px",
        }
      }, "👋Hello"),
    ul(li("🗺️World"), li(a({ href: "https://vanjs.org/" }, "🍦VanJS"))),
  );
}

btw: I've been thinking about this feature for a long time but haven't figure out a satisfactory way that is simple enough.

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

Successfully merging this pull request may close these issues.

2 participants