Skip to content

Commit c7bbe55

Browse files
feat: introduce axis parameter to allow for horizontal slide transition (#6183)
--------- Co-authored-by: Tan Li Hau <[email protected]>
1 parent b5ec863 commit c7bbe55

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

site/content/docs/04-run-time.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ Slides an element in and out.
768768
* `delay` (`number`, default 0) — milliseconds before starting
769769
* `duration` (`number`, default 400) — milliseconds the transition lasts
770770
* `easing` (`function`, default `cubicOut`) — an [easing function](/docs#run-time-svelte-easing)
771+
- `axis` (`x` | `y`, default `y`) — the axis of motion along which the transition occurs
771772

772773
```sv
773774
<script>
@@ -776,8 +777,8 @@ Slides an element in and out.
776777
</script>
777778
778779
{#if condition}
779-
<div transition:slide="{{delay: 250, duration: 300, easing: quintOut }}">
780-
slides in and out
780+
<div transition:slide="{{delay: 250, duration: 300, easing: quintOut, axis: 'x'}}">
781+
slides in and out horizontally
781782
</div>
782783
{/if}
783784
```

src/runtime/transition/index.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,37 +98,41 @@ export interface SlideParams {
9898
delay?: number;
9999
duration?: number;
100100
easing?: EasingFunction;
101+
axis?: 'x' | 'y';
101102
}
102103

103104
export function slide(node: Element, {
104105
delay = 0,
105106
duration = 400,
106-
easing = cubicOut
107+
easing = cubicOut,
108+
axis = 'y'
107109
}: SlideParams = {}): TransitionConfig {
108110
const style = getComputedStyle(node);
109111
const opacity = +style.opacity;
110-
const height = parseFloat(style.height);
111-
const padding_top = parseFloat(style.paddingTop);
112-
const padding_bottom = parseFloat(style.paddingBottom);
113-
const margin_top = parseFloat(style.marginTop);
114-
const margin_bottom = parseFloat(style.marginBottom);
115-
const border_top_width = parseFloat(style.borderTopWidth);
116-
const border_bottom_width = parseFloat(style.borderBottomWidth);
117-
112+
const primary_property = axis === 'y' ? 'height' : 'width';
113+
const primary_property_value = parseFloat(style[primary_property]);
114+
const secondary_properties = axis === 'y' ? ['top', 'bottom'] : ['left', 'right'];
115+
const capitalized_secondary_properties = secondary_properties.map((e) => `${e[0].toUpperCase()}${e.slice(1)}`);
116+
const padding_start_value = parseFloat(style[`padding${capitalized_secondary_properties[0]}`]);
117+
const padding_end_value = parseFloat(style[`padding${capitalized_secondary_properties[1]}`]);
118+
const margin_start_value = parseFloat(style[`margin${capitalized_secondary_properties[0]}`]);
119+
const margin_end_value = parseFloat(style[`margin${capitalized_secondary_properties[1]}`]);
120+
const border_width_start_value = parseFloat(style[`border${capitalized_secondary_properties[0]}Width`]);
121+
const border_width_end_value = parseFloat(style[`border${capitalized_secondary_properties[1]}Width`]);
118122
return {
119123
delay,
120124
duration,
121125
easing,
122126
css: t =>
123127
'overflow: hidden;' +
124128
`opacity: ${Math.min(t * 20, 1) * opacity};` +
125-
`height: ${t * height}px;` +
126-
`padding-top: ${t * padding_top}px;` +
127-
`padding-bottom: ${t * padding_bottom}px;` +
128-
`margin-top: ${t * margin_top}px;` +
129-
`margin-bottom: ${t * margin_bottom}px;` +
130-
`border-top-width: ${t * border_top_width}px;` +
131-
`border-bottom-width: ${t * border_bottom_width}px;`
129+
`${primary_property}: ${t * primary_property_value}px;` +
130+
`padding-${secondary_properties[0]}: ${t * padding_start_value}px;` +
131+
`padding-${secondary_properties[1]}: ${t * padding_end_value}px;` +
132+
`margin-${secondary_properties[0]}: ${t * margin_start_value}px;` +
133+
`margin-${secondary_properties[1]}: ${t * margin_end_value}px;` +
134+
`border-${secondary_properties[0]}-width: ${t * border_width_start_value}px;` +
135+
`border-${secondary_properties[1]}-width: ${t * border_width_end_value}px;`
132136
};
133137
}
134138

0 commit comments

Comments
 (0)