basic attachments working

pull/15045/head
Rich Harris 9 months ago
parent 531e75d314
commit b29e1e3a6f

@ -56,6 +56,7 @@ import { TitleElement } from './visitors/TitleElement.js';
import { TransitionDirective } from './visitors/TransitionDirective.js';
import { UpdateExpression } from './visitors/UpdateExpression.js';
import { UseDirective } from './visitors/UseDirective.js';
import { UseTag } from './visitors/UseTag.js';
import { VariableDeclaration } from './visitors/VariableDeclaration.js';
/** @type {Visitors} */
@ -131,6 +132,7 @@ const visitors = {
TransitionDirective,
UpdateExpression,
UseDirective,
UseTag,
VariableDeclaration
};

@ -82,7 +82,7 @@ export function RegularElement(node, context) {
/** @type {AST.StyleDirective[]} */
const style_directives = [];
/** @type {Array<AST.AnimateDirective | AST.BindDirective | AST.OnDirective | AST.TransitionDirective | AST.UseDirective>} */
/** @type {Array<AST.AnimateDirective | AST.BindDirective | AST.OnDirective | AST.TransitionDirective | AST.UseDirective | AST.UseTag>} */
const other_directives = [];
/** @type {ExpressionStatement[]} */
@ -152,6 +152,10 @@ export function RegularElement(node, context) {
has_use = true;
other_directives.push(attribute);
break;
case 'UseTag':
other_directives.push(attribute);
break;
}
}

@ -0,0 +1,21 @@
/** @import { Expression } from 'estree' */
/** @import { AST } from '#compiler' */
/** @import { ComponentContext } from '../types' */
import * as b from '../../../../utils/builders.js';
/**
* @param {AST.UseTag} node
* @param {ComponentContext} context
*/
export function UseTag(node, context) {
context.state.init.push(
b.stmt(
b.call(
'$.attach',
context.state.node,
b.thunk(/** @type {Expression} */ (context.visit(node.expression)))
)
)
);
context.next();
}

@ -553,6 +553,7 @@ export namespace AST {
| AST.Attribute
| AST.SpreadAttribute
| Directive
| AST.UseTag
| AST.Comment
| Block;

@ -0,0 +1,11 @@
import { effect } from '../../reactivity/effects.js';
/**
* @param {Element} node
* @param {() => (node: Element) => void} get_fn
*/
export function attach(node, get_fn) {
effect(() => {
get_fn()(node);
});
}

@ -27,6 +27,7 @@ export { element } from './dom/blocks/svelte-element.js';
export { head } from './dom/blocks/svelte-head.js';
export { append_styles } from './dom/css.js';
export { action } from './dom/elements/actions.js';
export { attach } from './dom/elements/attachments.js';
export {
remove_input_defaults,
set_attribute,

@ -0,0 +1,6 @@
import { test } from '../../test';
export default test({
ssrHtml: `<div></div>`,
html: `<div>DIV</div>`
});

@ -0,0 +1 @@
<div {@use (node) => node.textContent = node.nodeName}></div>
Loading…
Cancel
Save