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

Transition internals #4999

Closed
wants to merge 12 commits into from
Closed
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
19 changes: 10 additions & 9 deletions src/compiler/compile/render_dom/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ export default class Block {
this.has_animation = true;
}

group_transition_out(fn) {
return b`@group_transition_out((#transition_out) => { ${fn(x`#transition_out`)} })`;
}

add_variable(id: Identifier, init?: Node) {
if (this.variables.has(id.name)) {
throw new Error(
Expand All @@ -230,17 +234,14 @@ export default class Block {
get_contents(key?: any) {
const { dev } = this.renderer.options;

if (this.has_outros) {
if (this.has_outros || this.has_intros) {
this.add_variable({ type: 'Identifier', name: '#current' });

if (this.chunks.intro.length > 0) {
this.chunks.intro.push(b`#current = true;`);
this.chunks.mount.push(b`#current = true;`);
}
this.chunks.intro.push(b`#current = true;`);
this.chunks.mount.push(b`#current = true;`);


if (this.chunks.outro.length > 0) {
this.chunks.outro.push(b`#current = false;`);
}
this.chunks.outro.push(b`#current = false;`);
}

if (this.autofocus) {
Expand Down Expand Up @@ -341,7 +342,7 @@ export default class Block {
properties.intro = noop;
} else {
properties.intro = x`function #intro(#local) {
${this.has_outros && b`if (#current) return;`}
${b`if (#current) return;`}
${this.chunks.intro}
}`;
}
Expand Down
38 changes: 13 additions & 25 deletions src/compiler/compile/render_dom/wrappers/EachBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import FragmentWrapper from './Fragment';
import { b, x } from 'code-red';
import ElseBlock from '../../nodes/ElseBlock';
import { Identifier, Node } from 'estree';
import bit_state from '../../utils/bit_state';

export class ElseBlockWrapper extends Wrapper {
node: ElseBlock;
Expand Down Expand Up @@ -423,25 +424,18 @@ export default class EachBlockWrapper extends Wrapper {

const dynamic = this.block.has_update_method;

const destroy = this.node.has_animation
? (this.block.has_outros
? `@fix_and_outro_and_destroy_block`
: `@fix_and_destroy_block`)
: this.block.has_outros
? `@outro_and_destroy_block`
: `@destroy_block`;
const transition_state = bit_state([dynamic, this.node.has_animation, this.block.has_outros]);
const update_keyed_each = (transition_out) =>
b`${iterations} = @update_keyed_each(${iterations}, #dirty, #ctx, ${transition_state}, ${get_key}, ${this.vars.each_block_value}, ${lookup}, ${update_mount_node}, ${create_each_block}, ${update_anchor_node}, ${this.vars.get_each_context}, ${transition_out});`;

if (this.dependencies.size) {
this.updates.push(b`
const ${this.vars.each_block_value} = ${snippet};
${this.renderer.options.dev && b`@validate_each_argument(${this.vars.each_block_value});`}

${this.block.has_outros && b`@group_outros();`}
${this.node.has_animation && b`for (let #i = 0; #i < ${view_length}; #i += 1) ${iterations}[#i].r();`}
${this.renderer.options.dev && b`@validate_each_keys(#ctx, ${this.vars.each_block_value}, ${this.vars.get_each_context}, ${get_key});`}
${iterations} = @update_keyed_each(${iterations}, #dirty, ${get_key}, ${dynamic ? 1 : 0}, #ctx, ${this.vars.each_block_value}, ${lookup}, ${update_mount_node}, ${destroy}, ${create_each_block}, ${update_anchor_node}, ${this.vars.get_each_context});
${(this.block.has_outros ? this.block.group_transition_out : v => v(null))(update_keyed_each)}
${this.node.has_animation && b`for (let #i = 0; #i < ${view_length}; #i += 1) ${iterations}[#i].a();`}
${this.block.has_outros && b`@check_outros();`}
`);
}

Expand Down Expand Up @@ -552,20 +546,14 @@ export default class EachBlockWrapper extends Wrapper {
let remove_old_blocks;

if (this.block.has_outros) {
const out = block.get_unique_name('out');

block.chunks.init.push(b`
const ${out} = i => @transition_out(${iterations}[i], 1, 1, () => {
${iterations}[i] = null;
});
`);
remove_old_blocks = b`
@group_outros();
for (#i = ${data_length}; #i < ${view_length}; #i += 1) {
${out}(#i);
}
@check_outros();
`;
remove_old_blocks = this.block.group_transition_out((transition_out) =>
b`for (#i = ${data_length}; #i < ${view_length}; #i += 1) {
const #index = #i;
${transition_out}(${iterations}[#index], () => {
${iterations}[#index] = null;
});
}`
);
} else {
remove_old_blocks = b`
for (${this.block.has_update_method ? null : x`#i = ${data_length}`}; #i < ${this.block.has_update_method ? view_length : '#old_length'}; #i += 1) {
Expand Down
239 changes: 100 additions & 139 deletions src/compiler/compile/render_dom/wrappers/Element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { Identifier } from 'estree';
import EventHandler from './EventHandler';
import { extract_names } from 'periscopic';
import Action from '../../../nodes/Action';
import Transition from '../../../nodes/Transition';

const events = [
{
Expand Down Expand Up @@ -379,8 +380,18 @@ export default class ElementWrapper extends Wrapper {

this.add_attributes(block);
this.add_directives_in_order(block);
this.add_transitions(block);
this.add_animation(block);
const { intro, outro } = this.node;
if (intro || outro) {
if (intro === outro) {
this.add_bidi_transition(block, intro);
} else {
this.add_intro(block, intro, outro);
this.add_outro(block, intro, outro);
}
}
if (this.node.animation) {
this.add_animation(block, intro);
}
this.add_classes(block);
this.add_manual_style_scoping(block);

Expand Down Expand Up @@ -728,165 +739,98 @@ export default class ElementWrapper extends Wrapper {
}
}

add_transitions(
block: Block
) {
const { intro, outro } = this.node;
if (!intro && !outro) return;

if (intro === outro) {
// bidirectional transition
const name = block.get_unique_name(`${this.var.name}_transition`);
const snippet = intro.expression
? intro.expression.manipulate(block)
: x`{}`;

block.add_variable(name);

const fn = this.renderer.reference(intro.name);

const intro_block = b`
@add_render_callback(() => {
if (!${name}) ${name} = @create_bidirectional_transition(${this.var}, ${fn}, ${snippet}, true);
${name}.run(1);
});
`;

const outro_block = b`
if (!${name}) ${name} = @create_bidirectional_transition(${this.var}, ${fn}, ${snippet}, false);
${name}.run(0);
`;

if (intro.is_local) {
block.chunks.intro.push(b`
if (#local) {
${intro_block}
}
`);

block.chunks.outro.push(b`
if (#local) {
${outro_block}
}
`);
} else {
block.chunks.intro.push(intro_block);
block.chunks.outro.push(outro_block);
}

block.chunks.destroy.push(b`if (detaching && ${name}) ${name}.end();`);
add_bidi_transition(block: Block, intro: Transition) {
const transition = block.get_unique_name(`${this.var.name}_transition`);
const snippet = intro.expression ? intro.expression.manipulate(block) : null;
const fn = this.renderer.reference(intro.name);


block.add_variable(transition);
block.chunks.hydrate.push(b`${transition} = @create_bidirectional_transition(${this.var}, ${fn}, ${snippet});`);
if (intro.expression) {
const dirty = block.renderer.dirty([intro.name,...Array.from(intro.expression.dependencies)]);
block.chunks.update.push(b`if (${dirty}) ${transition}.u(${fn}, ${snippet});`);
}
let intro_block = b`${transition}.i();`;
let outro_block = b`${transition}.o();`;

else {
const intro_name = intro && block.get_unique_name(`${this.var.name}_intro`);
const outro_name = outro && block.get_unique_name(`${this.var.name}_outro`);

if (intro) {
block.add_variable(intro_name);
const snippet = intro.expression
? intro.expression.manipulate(block)
: x`{}`;

const fn = this.renderer.reference(intro.name);

let intro_block;

if (outro) {
intro_block = b`
@add_render_callback(() => {
if (${outro_name}) ${outro_name}.end(1);
if (!${intro_name}) ${intro_name} = @create_in_transition(${this.var}, ${fn}, ${snippet});
${intro_name}.start();
});
`;

block.chunks.outro.push(b`if (${intro_name}) ${intro_name}.invalidate();`);
} else {
intro_block = b`
if (!${intro_name}) {
@add_render_callback(() => {
${intro_name} = @create_in_transition(${this.var}, ${fn}, ${snippet});
${intro_name}.start();
});
}
`;
}
if (intro.is_local) {
intro_block = b`if (#local) { ${intro_block} }`;
outro_block = b`if (#local) { ${outro_block} }`;
}
block.chunks.intro.push(intro_block);
block.chunks.outro.push(outro_block);

if (intro.is_local) {
intro_block = b`
if (#local) {
${intro_block}
}
`;
block.chunks.destroy.push(b`if (detaching && ${transition}) ${transition}.d();`);
}
add_intro(block: Block, intro: Transition, outro: Transition) {
if (outro) {
const outro_var = block.alias(`${this.var.name}_outro`);
block.chunks.intro.push(b`${outro_var}(1);`);
}
if (this.node.animation) {
const [unfreeze_var] = run_animation(this, block);
block.chunks.intro.push(b`
if (${unfreeze_var}) {
${unfreeze_var}();
${unfreeze_var} = void 0;
}
`);
}
if (!intro) return;

block.chunks.intro.push(intro_block);
}

if (outro) {
block.add_variable(outro_name);
const snippet = outro.expression
? outro.expression.manipulate(block)
: x`{}`;

const fn = this.renderer.reference(outro.name);

if (!intro) {
block.chunks.intro.push(b`
if (${outro_name}) ${outro_name}.end(1);
`);
}
const [intro_var, node, transitionFn, params] = run_transition(this, block, intro, `intro`);
block.add_variable(intro_var, x`@noop`);

// TODO hide elements that have outro'd (unless they belong to a still-outroing
// group) prior to their removal from the DOM
let outro_block = b`
${outro_name} = @create_out_transition(${this.var}, ${fn}, ${snippet});
`;
let start_intro = b`
${intro_var}();
${intro_var} = @run_in(${node}, ${transitionFn}, ${params});
`;
if (intro.is_local) start_intro = b`if (#local) { ${start_intro} }`;
block.chunks.intro.push(start_intro);
}
// TODO
// hide elements that have outro'd prior to their removal from the DOM
// ( ...unless they belong to a still-outroing group )
add_outro(block: Block, _intro: Transition, outro: Transition) {
if (!outro) return;

if (outro.is_local) {
outro_block = b`
if (#local) {
${outro_block}
}
`;
}
const [outro_var, node, transitionFn, params] = run_transition(this, block, outro, `outro`);
block.add_variable(outro_var, x`@noop`);

block.chunks.outro.push(outro_block);
let start_outro = b`${outro_var} = @run_out(${node}, ${transitionFn}, ${params});`;
if (outro.is_local) start_outro = b`if (#local) { ${start_outro} }`;
block.chunks.outro.push(start_outro);

block.chunks.destroy.push(b`if (detaching && ${outro_name}) ${outro_name}.end();`);
}
}
block.chunks.destroy.push(b`if (detaching) ${outro_var}();`);
}

add_animation(block: Block) {
if (!this.node.animation) return;
add_animation(block: Block, _intro: Transition) {

const { outro } = this.node;
const [unfreeze_var, rect_var, stop_animation_var, name_var, params_var] = run_animation(this, block);

const rect = block.get_unique_name('rect');
const stop_animation = block.get_unique_name('stop_animation');

block.add_variable(rect);
block.add_variable(stop_animation, x`@noop`);
block.add_variable(unfreeze_var);
block.add_variable(rect_var);
block.add_variable(stop_animation_var, x`@noop`);

block.chunks.measure.push(b`
${rect} = ${this.var}.getBoundingClientRect();
${rect_var} = ${this.var}.getBoundingClientRect();
`);

block.chunks.fix.push(b`
@fix_position(${this.var});
${stop_animation}();
${outro && b`@add_transform(${this.var}, ${rect});`}
${stop_animation_var}();
${unfreeze_var} = @fix_position(${this.var}, ${rect_var});
`);

const params = this.node.animation.expression ? this.node.animation.expression.manipulate(block) : x`{}`;

const name = this.renderer.reference(this.node.animation.name);

block.chunks.animate.push(b`
${stop_animation}();
${stop_animation} = @create_animation(${this.var}, ${rect}, ${name}, ${params});
if (${unfreeze_var} || !${rect_var}) return
else {
${stop_animation_var}();
${stop_animation_var} = @run_animation(${this.var}, ${rect_var}, ${name_var}, ${params_var});
}
`);

block.chunks.destroy.push(b`${unfreeze_var} = void 0;`);
}

add_classes(block: Block) {
Expand Down Expand Up @@ -995,3 +939,20 @@ function to_html(wrappers: Array<ElementWrapper | TextWrapper | TagWrapper>, blo
}
});
}
function run_animation(element: ElementWrapper, block: Block) {
return [
block.alias('unfreeze'),
block.alias('rect'),
block.alias('stop_animation'),
element.renderer.reference(element.node.animation.name),
element.node.animation.expression ? element.node.animation.expression.manipulate(block) : null,
];
}
function run_transition(element: ElementWrapper, block: Block, transition: Transition, type: string) {
return [
/* node_intro */ block.alias(`${element.var.name}_${type}`),
/* node */ element.var,
/* transitionFn */ element.renderer.reference(transition.name),
/* params */ transition.expression ? transition.expression.manipulate(block) : null,
];
}
Loading