Skip to content

chore: simplify transitions #10798

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 167 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
167 commits
Select commit Hold shift + click to select a range
edbf57a
replace transition code
Rich-Harris Mar 13, 2024
776fe8b
get rid of some stuff
Rich-Harris Mar 13, 2024
74cf046
simplify
Rich-Harris Mar 13, 2024
e8542f9
remove some junk
Rich-Harris Mar 13, 2024
03a54fa
not sure how we solved this before, but i guess this makes sense
Rich-Harris Mar 13, 2024
dadf7a5
oh hey i don't think we need this
Rich-Harris Mar 13, 2024
14c6563
make elseif transparent for transition purposes
Rich-Harris Mar 13, 2024
bf476cc
oops
Rich-Harris Mar 14, 2024
807ffcb
edge case
Rich-Harris Mar 14, 2024
357644e
fix
Rich-Harris Mar 14, 2024
43e893c
do not want
Rich-Harris Mar 14, 2024
c7450bb
rename
Rich-Harris Mar 14, 2024
86019c6
transition out ecah blocks when emptying
Rich-Harris Mar 14, 2024
eca5c70
baby steps
Rich-Harris Mar 14, 2024
93c682d
hydration fix
Rich-Harris Mar 14, 2024
cf42a60
tidy up
Rich-Harris Mar 14, 2024
30ac645
tidy up
Rich-Harris Mar 14, 2024
d589fb6
tidy up
Rich-Harris Mar 14, 2024
ba1279d
fallbacks
Rich-Harris Mar 14, 2024
255b98c
man i love deleting code
Rich-Harris Mar 14, 2024
72a99ee
tidy up
Rich-Harris Mar 14, 2024
e15875b
note to self
Rich-Harris Mar 14, 2024
4802872
why was this an effect
Rich-Harris Mar 14, 2024
684da13
tidy up
Rich-Harris Mar 15, 2024
6a72a4f
tidy up
Rich-Harris Mar 15, 2024
6f8ccb9
key blocks
Rich-Harris Mar 15, 2024
0dc7297
temporary
Rich-Harris Mar 15, 2024
0cc47a6
fix
Rich-Harris Mar 15, 2024
0121aba
WIP
Rich-Harris Mar 15, 2024
f297102
fix
Rich-Harris Mar 16, 2024
1cff19a
simplify
Rich-Harris Mar 16, 2024
43e6e9d
emit events
Rich-Harris Mar 16, 2024
fbf8308
delete delete delete
Rich-Harris Mar 16, 2024
f2e5832
destroy child effects
Rich-Harris Mar 17, 2024
b62ad7b
simplify helper
Rich-Harris Mar 17, 2024
1dae1a4
simplify helper
Rich-Harris Mar 17, 2024
36feeb3
fix
Rich-Harris Mar 17, 2024
dc18c5d
remove commented out code
Rich-Harris Mar 17, 2024
f19255c
fix wonky test
Rich-Harris Mar 17, 2024
36b8cbf
fix test
Rich-Harris Mar 17, 2024
26f75d9
fix test
Rich-Harris Mar 17, 2024
0977ae0
fix test
Rich-Harris Mar 17, 2024
cf1fcd4
dynamic components
Rich-Harris Mar 17, 2024
2fb3cfc
fix test
Rich-Harris Mar 18, 2024
e64f9af
await
Rich-Harris Mar 18, 2024
1c27148
tidy up
Rich-Harris Mar 18, 2024
1449eea
fix
Rich-Harris Mar 18, 2024
f39ef2a
fix
Rich-Harris Mar 18, 2024
72155c5
fix test
Rich-Harris Mar 18, 2024
a2c6f29
tidy up
Rich-Harris Mar 18, 2024
6443df9
we dont need to reconcile during hydration
Rich-Harris Mar 18, 2024
d8a4778
simplify
Rich-Harris Mar 18, 2024
b2b10a7
tidy up
Rich-Harris Mar 18, 2024
8641aeb
fix
Rich-Harris Mar 19, 2024
98cffcf
reduce indentation
Rich-Harris Mar 19, 2024
258c46b
simplify
Rich-Harris Mar 19, 2024
a576daf
remove some junk
Rich-Harris Mar 19, 2024
df56862
remove some junk
Rich-Harris Mar 19, 2024
1cc7470
simplify
Rich-Harris Mar 19, 2024
d4cd830
tidy up
Rich-Harris Mar 19, 2024
8e9b604
prefer while over do-while (this appears to have the same behaviour)
Rich-Harris Mar 19, 2024
b9857a5
group fast paths
Rich-Harris Mar 19, 2024
5836d6b
rename
Rich-Harris Mar 19, 2024
96eaf4a
unused import
Rich-Harris Mar 19, 2024
82b496d
unused exports
Rich-Harris Mar 19, 2024
7f08bef
try this
Rich-Harris Mar 19, 2024
4a97674
simplify
Rich-Harris Mar 19, 2024
5768b83
simplify
Rich-Harris Mar 19, 2024
86bf52a
simplify
Rich-Harris Mar 19, 2024
6fb8af7
simplify
Rich-Harris Mar 19, 2024
2d521b6
tidy up
Rich-Harris Mar 19, 2024
4d50f5a
simplify
Rich-Harris Mar 19, 2024
34c32dd
simplify
Rich-Harris Mar 19, 2024
5312273
tidy up
Rich-Harris Mar 19, 2024
8bc8869
simplify
Rich-Harris Mar 19, 2024
aadb00a
simplify
Rich-Harris Mar 19, 2024
d9b088f
more compact names
Rich-Harris Mar 19, 2024
4a2c925
simplify
Rich-Harris Mar 19, 2024
278fafc
better comments
Rich-Harris Mar 19, 2024
eda7515
simplify
Rich-Harris Mar 19, 2024
2c942d8
tidy up
Rich-Harris Mar 19, 2024
6609ca0
we don't actually gain anything from this
Rich-Harris Mar 19, 2024
ab965b6
fix binding group order bug (revealed by previous commit, but exists …
Rich-Harris Mar 19, 2024
0f5f54d
tidy up
Rich-Harris Mar 19, 2024
ae72e2d
simplify
Rich-Harris Mar 19, 2024
c2481b2
tidy up
Rich-Harris Mar 19, 2024
138da44
remove some junk
Rich-Harris Mar 19, 2024
87b4fc3
simplify
Rich-Harris Mar 19, 2024
e001417
note to self
Rich-Harris Mar 19, 2024
c29a688
tidy up
Rich-Harris Mar 19, 2024
4de3341
revert this bit
Rich-Harris Mar 19, 2024
c55b143
tidy up
Rich-Harris Mar 19, 2024
4d6aa30
simplify
Rich-Harris Mar 19, 2024
32e5977
simplify
Rich-Harris Mar 19, 2024
20e19d0
simplify
Rich-Harris Mar 19, 2024
284b17d
symmetry
Rich-Harris Mar 19, 2024
d074000
tidy up
Rich-Harris Mar 19, 2024
0e4ee8e
var
Rich-Harris Mar 19, 2024
21286a9
rename some stuff
Rich-Harris Mar 19, 2024
683abe0
tidy up
Rich-Harris Mar 20, 2024
fd182df
simplify
Rich-Harris Mar 20, 2024
fd56c71
keyed each transitions
Rich-Harris Mar 20, 2024
8fcf1f1
make elements inert
Rich-Harris Mar 20, 2024
8ab8ae3
deferred transitions
Rich-Harris Mar 20, 2024
a7aef71
fix
Rich-Harris Mar 20, 2024
8e118d9
fix test
Rich-Harris Mar 20, 2024
2ee9f2f
fix some tests
Rich-Harris Mar 20, 2024
865f526
simplify
Rich-Harris Mar 20, 2024
a218c61
fix
Rich-Harris Mar 20, 2024
31abf5c
fix test
Rich-Harris Mar 20, 2024
4474625
fix
Rich-Harris Mar 20, 2024
a790092
eh that'll do for now
Rich-Harris Mar 20, 2024
cef695f
fix
Rich-Harris Mar 20, 2024
74c4f27
revert all these random changes
Rich-Harris Mar 20, 2024
5a991cf
fix
Rich-Harris Mar 20, 2024
080078d
fix
Rich-Harris Mar 20, 2024
bb4f414
simplify
Rich-Harris Mar 20, 2024
0851389
tidy up
Rich-Harris Mar 20, 2024
936adcc
simplify
Rich-Harris Mar 20, 2024
f6472bf
simplify
Rich-Harris Mar 20, 2024
1883b98
tidy up
Rich-Harris Mar 20, 2024
02a62a2
tidy up
Rich-Harris Mar 20, 2024
519573d
tidy up
Rich-Harris Mar 21, 2024
acb95e8
WIP
Rich-Harris Mar 21, 2024
e8769ef
WIP
Rich-Harris Mar 21, 2024
a149e80
working
Rich-Harris Mar 21, 2024
0e2be54
tidy up
Rich-Harris Mar 21, 2024
d21a725
fix
Rich-Harris Mar 21, 2024
8675f46
tidy up
Rich-Harris Mar 21, 2024
70b2da0
tidy up
Rich-Harris Mar 21, 2024
4df995c
lerp
Rich-Harris Mar 21, 2024
a9266bb
tidy up
Rich-Harris Mar 21, 2024
884110a
rename
Rich-Harris Mar 21, 2024
fa34637
rename
Rich-Harris Mar 21, 2024
628df85
almost everything working
Rich-Harris Mar 21, 2024
4704ccf
tidy up
Rich-Harris Mar 21, 2024
86eef35
ALL TESTS PASSING
Rich-Harris Mar 21, 2024
8863459
Merge branch 'main' into simplify-transitions
Rich-Harris Mar 21, 2024
818dace
Merge branch 'main' into simplify-transitions
Rich-Harris Mar 21, 2024
b2ca971
fix treeshaking
Rich-Harris Mar 21, 2024
fa1b3a8
Apply suggestions from code review
Rich-Harris Mar 21, 2024
2bcee65
comment
Rich-Harris Mar 21, 2024
a199e5a
explain elseif locality
Rich-Harris Mar 21, 2024
0b4850a
explain flushSync
Rich-Harris Mar 21, 2024
e40f30c
comments
Rich-Harris Mar 21, 2024
412f4c6
this is accounted for
Rich-Harris Mar 21, 2024
e605941
add some comments
Rich-Harris Mar 21, 2024
8fe0cbb
remove outdated comment
Rich-Harris Mar 21, 2024
f50d5f2
add comment
Rich-Harris Mar 21, 2024
ac0c8b3
add comments
Rich-Harris Mar 21, 2024
230a33d
rename
Rich-Harris Mar 21, 2024
bdcd25e
a few naming tweaks
Rich-Harris Mar 21, 2024
d102d4e
explain each_item_block stuff
Rich-Harris Mar 21, 2024
3f3c7b0
remove unused arg
Rich-Harris Mar 21, 2024
adfd6f0
optimise
Rich-Harris Mar 21, 2024
f2aba65
add some comments
Rich-Harris Mar 21, 2024
2921ca3
fix test post-optimisation
Rich-Harris Mar 21, 2024
21f9f3c
explicit comparisons
Rich-Harris Mar 21, 2024
e5cc4c0
some docs
Rich-Harris Mar 21, 2024
d3ef8a9
fix intro events
Rich-Harris Mar 21, 2024
aa64002
move effect.ran into the bitmask
Rich-Harris Mar 21, 2024
0fdd50d
docs
Rich-Harris Mar 21, 2024
88b288a
rename run_transitions to should_intro, add explanatory jsdoc
Rich-Harris Mar 21, 2024
38cc2a6
add some more docs
Rich-Harris Mar 21, 2024
88d1287
remove animation before measuring
Rich-Harris Mar 21, 2024
8738be0
only animate blocks that persist
Rich-Harris Mar 21, 2024
b98b6c7
note to self
Rich-Harris Mar 21, 2024
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
2 changes: 2 additions & 0 deletions packages/svelte/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
/motion.d.ts
/store.d.ts
/transition.d.ts

/scripts/_bundle.js
8 changes: 5 additions & 3 deletions packages/svelte/scripts/check-treeshakeability.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ for (const key in pkg.exports) {
}

const client_main = path.resolve(pkg.exports['.'].browser);
const without_hydration = await bundle_code(
const bundle = await bundle_code(
// Use all features which contain hydration code to ensure it's treeshakeable
compile(
`
Expand Down Expand Up @@ -109,15 +109,17 @@ const without_hydration = await bundle_code(
).js.code
);

if (!without_hydration.includes('current_hydration_fragment')) {
if (!bundle.includes('current_hydration_fragment')) {
// eslint-disable-next-line no-console
console.error(`✅ Hydration code treeshakeable`);
} else {
// eslint-disable-next-line no-console
console.error(without_hydration);
console.error(bundle);
// eslint-disable-next-line no-console
console.error(`❌ Hydration code not treeshakeable`);
failed = true;

fs.writeFileSync('scripts/_bundle.js', bundle);
}

// eslint-disable-next-line no-console
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ import {
AttributeAliases,
DOMBooleanAttributes,
EACH_INDEX_REACTIVE,
EACH_IS_ANIMATED,
EACH_IS_CONTROLLED,
EACH_IS_STRICT_EQUALS,
EACH_ITEM_REACTIVE,
EACH_KEYED
EACH_KEYED,
TRANSITION_GLOBAL,
TRANSITION_IN,
TRANSITION_OUT
} from '../../../../../constants.js';
import { regex_is_valid_identifier } from '../../../patterns.js';
import { javascript_visitors_runes } from './javascript-runes.js';
Expand Down Expand Up @@ -1922,7 +1926,7 @@ export const template_visitors = {
state.init.push(
b.stmt(
b.call(
'$.animate',
'$.animation',
state.node,
b.thunk(
/** @type {import('estree').Expression} */ (visit(parse_directive_name(node.name)))
Expand All @@ -1939,25 +1943,21 @@ export const template_visitors = {
error(node, 'INTERNAL', 'Node should have been handled elsewhere');
},
TransitionDirective(node, { state, visit }) {
const type = node.intro && node.outro ? '$.transition' : node.intro ? '$.in' : '$.out';
const expression =
node.expression === null
? b.literal(null)
: b.thunk(/** @type {import('estree').Expression} */ (visit(node.expression)));
let flags = node.modifiers.includes('global') ? TRANSITION_GLOBAL : 0;
if (node.intro) flags |= TRANSITION_IN;
if (node.outro) flags |= TRANSITION_OUT;

state.init.push(
b.stmt(
b.call(
type,
state.node,
b.thunk(
/** @type {import('estree').Expression} */ (visit(parse_directive_name(node.name)))
),
expression,
node.modifiers.includes('global') ? b.true : b.false
)
)
);
const args = [
b.literal(flags),
state.node,
b.thunk(/** @type {import('estree').Expression} */ (visit(parse_directive_name(node.name))))
];

if (node.expression) {
args.push(b.thunk(/** @type {import('estree').Expression} */ (visit(node.expression))));
}

state.init.push(b.stmt(b.call('$.transition', ...args)));
},
RegularElement(node, context) {
if (node.name === 'noscript') {
Expand Down Expand Up @@ -2345,6 +2345,19 @@ export const template_visitors = {
each_type |= EACH_ITEM_REACTIVE;
}

// Since `animate:` can only appear on elements that are the sole child of a keyed each block,
// we can determine at compile time whether the each block is animated or not (in which
// case it should measure animated elements before and after reconciliation).
if (
node.key &&
node.body.nodes.some((child) => {
if (child.type !== 'RegularElement' && child.type !== 'SvelteElement') return false;
return child.attributes.some((attr) => attr.type === 'AnimateDirective');
})
) {
each_type |= EACH_IS_ANIMATED;
}

if (each_node_meta.is_controlled) {
each_type |= EACH_IS_CONTROLLED;
}
Expand Down Expand Up @@ -2557,22 +2570,44 @@ export const template_visitors = {
context.visit(node.consequent)
);

context.state.after_update.push(
b.stmt(
b.call(
'$.if',
context.state.node,
b.thunk(/** @type {import('estree').Expression} */ (context.visit(node.test))),
b.arrow([b.id('$$anchor')], consequent),
node.alternate
? b.arrow(
[b.id('$$anchor')],
/** @type {import('estree').BlockStatement} */ (context.visit(node.alternate))
)
: b.literal(null)
)
)
);
const args = [
context.state.node,
b.thunk(/** @type {import('estree').Expression} */ (context.visit(node.test))),
b.arrow([b.id('$$anchor')], consequent),
node.alternate
? b.arrow(
[b.id('$$anchor')],
/** @type {import('estree').BlockStatement} */ (context.visit(node.alternate))
)
: b.literal(null)
];

if (node.elseif) {
// We treat this...
//
// {#if x}
// ...
// {:else}
// {#if y}
// <div transition:foo>...</div>
// {/if}
// {/if}
//
// ...slightly differently to this...
//
// {#if x}
// ...
// {:else if y}
// <div transition:foo>...</div>
// {/if}
//
// ...even though they're logically equivalent. In the first case, the
// transition will only play when `y` changes, but in the second it
// should play when `x` or `y` change — both are considered 'local'
args.push(b.literal(true));
}

context.state.after_update.push(b.stmt(b.call('$.if', ...args)));
},
AwaitBlock(node, context) {
context.state.template.push('<!>');
Expand Down
4 changes: 4 additions & 0 deletions packages/svelte/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export const PROPS_IS_RUNES = 1 << 1;
export const PROPS_IS_UPDATED = 1 << 2;
export const PROPS_IS_LAZY_INITIAL = 1 << 3;

export const TRANSITION_IN = 1;
export const TRANSITION_OUT = 1 << 1;
export const TRANSITION_GLOBAL = 1 << 2;

/** List of Element events that will be delegated */
export const DelegatedEvents = [
'beforeinput',
Expand Down
13 changes: 2 additions & 11 deletions packages/svelte/src/internal/client/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,8 @@ export const DIRTY = 1 << 9;
export const MAYBE_DIRTY = 1 << 10;
export const INERT = 1 << 11;
export const DESTROYED = 1 << 12;

export const ROOT_BLOCK = 0;
export const IF_BLOCK = 1;
export const EACH_BLOCK = 2;
export const EACH_ITEM_BLOCK = 3;
export const AWAIT_BLOCK = 4;
export const KEY_BLOCK = 5;
export const HEAD_BLOCK = 6;
export const DYNAMIC_COMPONENT_BLOCK = 7;
export const DYNAMIC_ELEMENT_BLOCK = 8;
export const SNIPPET_BLOCK = 9;
export const IS_ELSEIF = 1 << 13;
export const EFFECT_RAN = 1 << 14;

export const UNINITIALIZED = Symbol();
export const STATE_SYMBOL = Symbol('$state');
Loading