Skip to content

Commit b9e8677

Browse files
committed
hidden with transition (poc)
1 parent 233bbec commit b9e8677

File tree

8 files changed

+51
-5
lines changed

8 files changed

+51
-5
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/RegularElement.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,8 @@ function build_element_attribute_update_assignment(element, node_id, attribute,
545545
update = b.stmt(b.call('$.set_value', node_id, value));
546546
} else if (name === 'checked') {
547547
update = b.stmt(b.call('$.set_checked', node_id, value));
548+
} else if (name === 'hidden') {
549+
update = b.stmt(b.call('$.set_hidden', node_id, value));
548550
} else if (is_dom_property(name)) {
549551
update = b.stmt(b.assignment('=', b.member(node_id, name), value));
550552
} else {

packages/svelte/src/compiler/phases/3-transform/client/visitors/TransitionDirective.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/** @import { Expression } from 'estree' */
22
/** @import { AST } from '#compiler' */
33
/** @import { ComponentContext } from '../types' */
4-
import { TRANSITION_GLOBAL, TRANSITION_IN, TRANSITION_OUT } from '../../../../../constants.js';
4+
import {
5+
TRANSITION_GLOBAL,
6+
TRANSITION_HIDDEN,
7+
TRANSITION_IN,
8+
TRANSITION_OUT
9+
} from '../../../../../constants.js';
510
import * as b from '../../../../utils/builders.js';
611
import { parse_directive_name } from './shared/utils.js';
712

@@ -13,6 +18,9 @@ export function TransitionDirective(node, context) {
1318
let flags = node.modifiers.includes('global') ? TRANSITION_GLOBAL : 0;
1419
if (node.intro) flags |= TRANSITION_IN;
1520
if (node.outro) flags |= TRANSITION_OUT;
21+
if (node.modifiers.includes('hidden')) {
22+
flags |= TRANSITION_HIDDEN;
23+
}
1624

1725
const args = [
1826
b.literal(flags),

packages/svelte/src/compiler/types/template.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ export namespace AST {
251251
name: string;
252252
/** The 'y' in `transition:x={y}` */
253253
expression: null | Expression;
254-
modifiers: Array<'local' | 'global'>;
254+
modifiers: Array<'local' | 'global' | 'hidden'>;
255255
/** True if this is a `transition:` or `in:` directive */
256256
intro: boolean;
257257
/** True if this is a `transition:` or `out:` directive */

packages/svelte/src/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const PROPS_IS_LAZY_INITIAL = 1 << 4;
1414
export const TRANSITION_IN = 1;
1515
export const TRANSITION_OUT = 1 << 1;
1616
export const TRANSITION_GLOBAL = 1 << 2;
17+
export const TRANSITION_HIDDEN = 1 << 3;
1718

1819
export const TEMPLATE_FRAGMENT = 1;
1920
export const TEMPLATE_USE_IMPORT_NODE = 1 << 1;

packages/svelte/src/internal/client/dom/elements/attributes.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,28 @@ export function set_checked(element, checked) {
7474
element.checked = checked;
7575
}
7676

77+
/**
78+
* @param {Element & { hidden: boolean, __tm?: import('#client').TransitionManager}} element
79+
* @param {boolean} hidden
80+
*/
81+
export function set_hidden(element, hidden) {
82+
// @ts-expect-error
83+
var attributes = (element.__attributes ??= {});
84+
85+
if (attributes.hidden === (attributes.hidden = !!hidden)) return;
86+
87+
if (element.__tm) {
88+
if (hidden) {
89+
element.__tm.out(() => (element.hidden = true));
90+
} else {
91+
element.hidden = false;
92+
element.__tm.in();
93+
}
94+
} else {
95+
element.hidden = hidden;
96+
}
97+
}
98+
7799
/**
78100
* @param {Element} element
79101
* @param {string} attribute

packages/svelte/src/internal/client/dom/elements/transitions.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import { active_effect, untrack } from '../../runtime.js';
55
import { loop } from '../../loop.js';
66
import { should_intro } from '../../render.js';
77
import { current_each_item } from '../blocks/each.js';
8-
import { TRANSITION_GLOBAL, TRANSITION_IN, TRANSITION_OUT } from '../../../../constants.js';
8+
import {
9+
TRANSITION_GLOBAL,
10+
TRANSITION_HIDDEN,
11+
TRANSITION_IN,
12+
TRANSITION_OUT
13+
} from '../../../../constants.js';
914
import { BLOCK_EFFECT, EFFECT_RAN, EFFECT_TRANSPARENT } from '../../constants.js';
1015
import { queue_micro_task } from '../task.js';
1116

@@ -169,6 +174,7 @@ export function transition(flags, element, get_fn, get_params) {
169174
var is_outro = (flags & TRANSITION_OUT) !== 0;
170175
var is_both = is_intro && is_outro;
171176
var is_global = (flags & TRANSITION_GLOBAL) !== 0;
177+
var is_hidden = (flags & TRANSITION_HIDDEN) !== 0;
172178

173179
/** @type {'in' | 'out' | 'both'} */
174180
var direction = is_both ? 'both' : is_intro ? 'in' : 'out';
@@ -243,6 +249,12 @@ export function transition(flags, element, get_fn, get_params) {
243249
}
244250
};
245251

252+
if (is_hidden) {
253+
// @ts-expect-error
254+
element.__tm = transition;
255+
return;
256+
}
257+
246258
var e = /** @type {Effect} */ (active_effect);
247259

248260
(e.transitions ??= []).push(transition);

packages/svelte/src/internal/client/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export {
3333
set_xlink_attribute,
3434
handle_lazy_img,
3535
set_value,
36-
set_checked
36+
set_checked,
37+
set_hidden
3738
} from './dom/elements/attributes.js';
3839
export { set_class, set_svg_class, set_mathml_class, toggle_class } from './dom/elements/class.js';
3940
export { apply, event, delegate, replay_events } from './dom/elements/events.js';

packages/svelte/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ declare module 'svelte/compiler' {
10601060
name: string;
10611061
/** The 'y' in `transition:x={y}` */
10621062
expression: null | Expression;
1063-
modifiers: Array<'local' | 'global'>;
1063+
modifiers: Array<'local' | 'global' | 'hidden'>;
10641064
/** True if this is a `transition:` or `in:` directive */
10651065
intro: boolean;
10661066
/** True if this is a `transition:` or `out:` directive */

0 commit comments

Comments
 (0)