-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[feat] Add options w/ direction to transitions #8068
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
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -971,7 +971,7 @@ transition:fn|local={params} | |
|
||
|
||
```js | ||
transition = (node: HTMLElement, params: any) => { | ||
transition = (node: HTMLElement, params: any, options: { direction: 'in' | 'out' | 'both' }) => { | ||
delay?: number, | ||
duration?: number, | ||
easing?: (t: number) => number, | ||
|
@@ -1091,6 +1091,32 @@ A custom transition function can also return a `tick` function, which is called | |
|
||
If a transition returns a function instead of a transition object, the function will be called in the next microtask. This allows multiple transitions to coordinate, making [crossfade effects](/tutorial/deferred-transitions) possible. | ||
|
||
Transition functions also receive a third argument, `options`, which contains information about the transition. | ||
|
||
Available values in the `options` object are: | ||
|
||
* `direction` - one of `in`, `out`, or `both` depending on the type of transition | ||
|
||
```sv | ||
<script> | ||
export let visible = false; | ||
|
||
function attribute(node, params, options) { | ||
node.dataset.direction = options.direction; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is a real-world example. For example, I think we will add the below example. export function my_transition(node, params, options) {
const { direction } = options;
if (direction === 'in') return wacky_intro_params(params);
if (direction === 'out') return wacky_outro_params(params);
return coordinated_outro(params);
} And according to other examples, these should work after just copying and pasting. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My real-world usecase is a niche embedded browser that doesn't support generating Neither of which is super easy to do in a small example, so I was trying to do something extremely short to get the idea across. I can make it do something when you copy and paste but it's gonna bloat out the example a bit 🫤 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Humm. I also thought example but I need 18 lines. <script>
import { slide, fade } from "svelte/transition";
let visible;
function custom_transition(node, params, options) {
const { direction } = options;
if (direction === "in") return () => slide(node, params);
if (direction === "out") return () => fade(node, params);
return null
}
</script>
<button on:click={() => (visible = !visible)}>Click Me</button>
{#if visible}
<div in:custom_transition={{ duration: 1000 }}>in</div>
{/if}
{#if visible}
<div out:custom_transition={{ duration: 500 }}>out</div>
{/if}
|
||
|
||
return { | ||
delay: params.delay || 0, | ||
duration: params.duration || 400, | ||
}; | ||
} | ||
</script> | ||
|
||
{#if visible} | ||
<div in:attribute> | ||
Attribute changes based on transition direction | ||
</div> | ||
{/if} | ||
``` | ||
|
||
##### Transition events | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export default { | ||
test({ assert, component, target }) { | ||
component.visible = true; | ||
|
||
const div_in = target.querySelector('#in'); | ||
const div_out = target.querySelector('#out'); | ||
const div_both = target.querySelector('#both'); | ||
|
||
assert.equal(div_in.initial, 'in'); | ||
assert.equal(div_out.initial, 'out'); | ||
assert.equal(div_both.initial, 'both'); | ||
|
||
return Promise.resolve().then(() => { | ||
assert.equal(div_in.later, 'in'); | ||
assert.equal(div_out.later, 'out'); | ||
assert.equal(div_both.later, 'both'); | ||
}); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<script> | ||
export let visible; | ||
|
||
function foo(node, _params, options) { | ||
node.initial = options.direction; | ||
|
||
return (opts) => { | ||
node.later = opts.direction; | ||
|
||
return { | ||
duration: 10 | ||
}; | ||
}; | ||
} | ||
</script> | ||
|
||
{#if visible} | ||
<div id="both" transition:foo></div> | ||
<div id="in" in:foo></div> | ||
{/if} | ||
|
||
{#if !visible} | ||
<div id="out" out:foo></div> | ||
{/if} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export default { | ||
test({ assert, component, target }) { | ||
component.visible = true; | ||
|
||
const div_in = target.querySelector('#in'); | ||
const div_out = target.querySelector('#out'); | ||
const div_both = target.querySelector('#both'); | ||
|
||
assert.equal(div_in.direction, 'in'); | ||
assert.equal(div_out.direction, 'out'); | ||
assert.equal(div_both.direction, 'both'); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<script> | ||
export let visible = false; | ||
|
||
function foo(node, _params, options) { | ||
node.direction = options.direction; | ||
|
||
return { | ||
duration: 10 | ||
}; | ||
} | ||
</script> | ||
|
||
{#if visible} | ||
<div id="both" transition:foo></div> | ||
<div id="in" in:foo></div> | ||
{/if} | ||
|
||
{#if !visible} | ||
<div id="out" out:foo></div> | ||
{/if} |
Uh oh!
There was an error while loading. Please reload this page.