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

feat(mount): pass attributes directly to component #48

Closed
wants to merge 16 commits into from

Conversation

sebkolind
Copy link
Collaborator

This is a proposal that allows passing attributes directly to a component via the mount function. It is useful for passing data to the component, that is available immediately on mount. Below is an example usage, where the parent component opens a modal at the click of a button.

The use case could also be if you fetch data from an endpoint in another component, and mounts a component with data from the response.

Usage

const Modal: Component = {
  view({ el }) {
    return div([
      p("I am the modal you are looking for."),
      // The attributes are now available on the `el`.
      p(`This is the id attribute: ${el.dataset.id}`),
      p(`This is the foo attribute: ${el.dataset.foo}`),
    ]);
  },
};

const DynamicMount: Component = {
  view() {
    return div([
      p("I am the parent component."),
      button("Open modal", {
        onclick() {
          mount(
            document.querySelector(".modal"),
            Modal,
            // Pass attributes to the component
            {
              ["data-id"]: "bar",
              ["data-foo"]: "bar",
            },
          );
        },
      }),
    ]);
  },
};

BREAKING CHANGE: This will remove the `attr` made available when using
`view` and `mounted`. Instead you can use `el.dataset` and type your
expected incoming data-* attributes with `Component<{}, Attrs>` and
define those in `Attrs`.
When looping use `for` loops instead, which _should_ be slightly faster.
Right?
All next tags will be released on the regular release workflow.
@sebkolind sebkolind added ☀️ enhancement New feature or request 🧪 experimental Things might explode labels Jun 4, 2024
@sebkolind sebkolind self-assigned this Jun 4, 2024
@sebkolind sebkolind requested a review from hyber1z0r June 4, 2024 18:24
src/attributes.ts Outdated Show resolved Hide resolved
@sebkolind sebkolind closed this Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
☀️ enhancement New feature or request 🧪 experimental Things might explode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants