Skip to content

fix: allow stores in transition,animation,use directives #10481

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

Merged
merged 7 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stale-jeans-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: subscribe to stores in `transition`,`animation`,`use` directives
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,25 @@ function is_store_name(name) {
return name[0] === '$' && /[A-Za-z_]/.test(name[1]);
}

/**
*
* @param {Iterable<import('#compiler').Binding>} bindings
*/
function store_sub_exist(bindings) {
for (const binding of bindings) {
if (binding.kind === 'store_sub') {
for (const reference of binding.references) {
const node = reference.path.at(-1);

// hacky way to ensure the sub is not in a directive e.g. use:$store as it is unneeded
if (node?.type !== 'RegularElement') {
return true;
}
}
}
}
}

/**
* @param {import('estree').AssignmentExpression} node
* @param {import('zimmerframe').Context<import('#compiler').SvelteNode, import('./types').ServerTransformState>} context
Expand Down Expand Up @@ -2089,15 +2108,10 @@ export function server_component(analysis, options) {
];
}

if (
[...analysis.instance.scope.declarations.values()].some(
(binding) => binding.kind === 'store_sub'
)
) {
if (store_sub_exist(analysis.instance.scope.declarations.values())) {
instance.body.unshift(b.const('$$store_subs', b.object([])));
template.body.push(b.stmt(b.call('$.unsubscribe_stores', b.id('$$store_subs'))));
}

// Propagate values of bound props upwards if they're undefined in the parent and have a value.
// Don't do this as part of the props retrieval because people could eagerly mutate the prop in the instance script.
/** @type {import('estree').Property[]} */
Expand Down
19 changes: 18 additions & 1 deletion packages/svelte/src/compiler/phases/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ export class Scope {
reference(node, path) {
path = [...path]; // ensure that mutations to path afterwards don't affect this reference
let references = this.references.get(node.name);

if (!references) this.references.set(node.name, (references = []));

references.push({ node, path });
Expand Down Expand Up @@ -329,6 +330,18 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
return /** @type {const} */ ([scope, is_default_slot]);
}

/**
* Reference store in transion:, use:, animate: directives
* @type {import('zimmerframe').Visitor<import('#compiler').Directive, State, import('#compiler').SvelteNode>}
*/
const SvelteDirective = (node, context) => {
const name = node.name;

if (name[0] === '$') {
context.state.scope.reference(b.id(name), context.path);
}
};

walk(ast, state, {
// references
Identifier(node, { path, state }) {
Expand Down Expand Up @@ -625,7 +638,11 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
)
]);
context.next();
}
},

TransitionDirective: SvelteDirective,
AnimateDirective: SvelteDirective,
UseDirective: SvelteDirective

// TODO others
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// main.svelte (Svelte VERSION)
// Note: compiler output will change before 5.0 is released!
import "svelte/internal/disclose-version";
import * as $ from "svelte/internal";
import { writable } from 'svelte/store';

var frag = $.template(`<div>Hello!</div> <div>Hello!</div>`, true);

export default function Main($$anchor, $$props) {
$.push($$props, false);

const $$subscriptions = {};

$.unsubscribe_on_destroy($$subscriptions);

const $animate = () => $.store_get(animate, "$animate", $$subscriptions);
const animate = writable();

$.init();

/* Init */
var fragment = $.open_frag($$anchor, true, frag);
var div = $.child_frag(fragment);

$.in(div, $animate, () => ({ duration: 750, x: 0, y: -200 }), false);

var div_1 = $.sibling($.sibling(div, true));

$.action(div_1, ($$node) => $animate()($$node));
$.close_frag($$anchor, fragment);
$.pop();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// main.svelte (Svelte VERSION)
// Note: compiler output will change before 5.0 is released!
import * as $ from "svelte/internal/server";
import { writable } from 'svelte/store';

export default function Main($$payload, $$props) {
$.push(false);

const animate = writable();

$$payload.out += `<div>Hello!</div> <div>Hello!</div>`;
$.pop();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
import { writable, } from 'svelte/store';

const animate = writable();
</script>

<div in:$animate={{ duration: 750, x:0, y: -200}}>Hello!</div>
<div use:$animate>Hello!</div>