{"id":19095641,"url":"https://github.com/evanminto/auto-slots","last_synced_at":"2026-06-25T09:31:34.407Z","repository":{"id":57112246,"uuid":"370209540","full_name":"evanminto/auto-slots","owner":"evanminto","description":null,"archived":false,"fork":false,"pushed_at":"2021-05-24T15:12:38.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-03T08:18:43.068Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/evanminto.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-05-24T02:52:21.000Z","updated_at":"2021-05-24T15:12:40.000Z","dependencies_parsed_at":"2022-08-21T00:00:24.611Z","dependency_job_id":null,"html_url":"https://github.com/evanminto/auto-slots","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evanminto%2Fauto-slots","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evanminto%2Fauto-slots/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evanminto%2Fauto-slots/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evanminto%2Fauto-slots/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/evanminto","download_url":"https://codeload.github.com/evanminto/auto-slots/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240145917,"owners_count":19755177,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T03:34:40.522Z","updated_at":"2026-05-26T09:30:19.839Z","avatar_url":"https://github.com/evanminto.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Auto Slots\n\nAuto Slots is a tiny mixin for web components that enables child elements to dynamically define their own slot names.\n\n## How It Works\n\nBy default, each slot name must be defined explicitly in both the Shadow DOM and Light DOM. There's no mechanism for assigning multiple elements to a slot and then looping over them. This makes it impossible to accept a list of children and then wrap _each child_ in a wrapper element inside the Shadow DOM.\n\nThis mixin solves the problem by querying a custom element's Light DOM children, finding slot names that match a prefix, and providing those slot names to the component as a getter. The component can then render the appropriate slots inside the Shadow DOM.\n\nEssentially this is an inversion of the default model. By default the Shadow DOM defines a finite set of slot names that the Light DOM consumes. With Auto Slots, the Light DOM defines a finite set of slot names (using a naming convention defined by the custom element) and then the Shadow DOM consumes that set.\n\n## Usage\n\nCurrently the only export is a higher-order component called `withAutoSlots`. In the future I may build a decorator version as well!\n\n```sh\nnpm install @evanminto/auto-slots\n```\n\nWith vanilla custom elements:\n\n```js\nimport { withAutoSlots } from '@evanminto/auto-slots';\n\nclass MyElement extends withAutoSlots(HTMLElement, { prefix: 'items[' }) {\n  connectedCallback() {\n    this.attachShadow({ mode: 'open' });\n\n    this.shadowRoot.innerHTML = `\n      \u003cul\u003e\n        ${this.autoSlotNames.map(slotName =\u003e `\n          \u003cli\u003e\u003cslot name=\"${slotName}\"\u003e\u003c/slot\u003e\u003c/li\u003e\n        `).join('')}\n      \u003c/ul\u003e\n    `;\n  }\n}\n\ncustomElements.define('my-element', MyElement);\n```\n\n```html\n\u003cmy-element\u003e\n  \u003cdiv slot=\"items[0]\"\u003eOne\u003c/div\u003e\n  \u003cdiv slot=\"items[1]\"\u003eTwo\u003c/div\u003e\n  \u003cdiv slot=\"items[2]\"\u003eThree\u003c/div\u003e\n\u003c/my-element\u003e\n```\n\nIt also works with [LitElement](https://lit.dev/) and any other library that outputs a custom element class:\n\n```js\nimport { html, LitElement } from 'lit';\nimport { withAutoSlots } from '@evanminto/auto-slots';\n\nclass MyElement extends withAutoSlots(LitElement, { prefix: 'items[' }) {\n  render() {\n    return html`\n      \u003cul\u003e\n        ${this.autoSlotNames.map(slotName =\u003e html`\n          \u003cli\u003e\u003cslot name=${slotName}\u003e\u003c/slot\u003e\u003c/li\u003e\n        `)}\n      \u003c/ul\u003e\n    `;\n  }\n}\n\ncustomElements.define('my-element', MyElement);\n```\n\n```html\n\u003cmy-element\u003e\n  \u003cdiv slot=\"items[0]\"\u003eOne\u003c/div\u003e\n  \u003cdiv slot=\"items[1]\"\u003eTwo\u003c/div\u003e\n  \u003cdiv slot=\"items[2]\"\u003eThree\u003c/div\u003e\n\u003c/my-element\u003e\n```\n\nYou can define a custom prefix, or leave it unset to use the default. By default, the prefix is the name of the custom element. For example:\n\n```js\nclass MyElement extends withAutoSlots(LitElement) {\n  // ...\n}\n\ncustomElements.define('my-element', MyElement);\n```\n\n```html\n\u003cmy-element\u003e\n  \u003cdiv slot=\"my-element1\"\u003eOne\u003c/div\u003e\n  \u003cdiv slot=\"my-element1\"\u003eTwo\u003c/div\u003e\n  \u003cdiv slot=\"my-element3\"\u003eThree\u003c/div\u003e\n\u003c/my-element\u003e\n```\n\n**Warning:** The list of slot names is dynamic, so it will always represent the current state of the slotable elements when you use the getter. However, this library doesn’t handle re-rendering your Shadow DOM HTML when the children change. You’ll have to set up your own Mutation Observer or use a different solution.\n\n## API\n\n### `withAutoSlots(ElementClass, { prefix })`\n\nHigher-order component. Call this on a custom element class and it will return a version of the class augmented with auto slots behavior.\n\n- `ElementClass` must be `HTMLElement` or a subclass of it. This is the class you want to mix the auto slots behavior into.\n- `prefix` must be a string. All slots that start with this value will be included in the auto slots list. *Defaults to the tag name of the custom element, lower-cased and hyphenated.*\n\n### `autoSlots`\n\nRead-only property on custom elements that have been augmented with `withAutoSlots()`.\n\nThe value is an array of strings. Each string is the name of the one of the slots matching the auto-slots prefix value.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevanminto%2Fauto-slots","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fevanminto%2Fauto-slots","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevanminto%2Fauto-slots/lists"}