Skip to content

Commit

Permalink
add react examples
Browse files Browse the repository at this point in the history
  • Loading branch information
claviska committed Nov 16, 2021
1 parent fe677f1 commit da7a177
Showing 1 changed file with 156 additions and 4 deletions.
160 changes: 156 additions & 4 deletions docs/components/animation.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,33 @@ To animate an element, wrap it in `<sl-animation>` and set an animation `name`.
</style>
```

```jsx react
import { SlAnimation } from '@shoelace-style/shoelace/dist/react';

const css = `
.animation-overview .box {
display: inline-block;
width: 100px;
height: 100px;
background-color: rgb(var(--sl-color-primary-600));
margin: 1.5rem;
}
`;

const App = () => (
<>
<div class="animation-overview">
<SlAnimation name="bounce" duration={2000} play><div class="box" /></SlAnimation>
<SlAnimation name="jello" duration={2000} play><div class="box" /></SlAnimation>
<SlAnimation name="heartBeat" duration={2000} play><div class="box" /></SlAnimation>
<SlAnimation name="flip" duration={2000} play><div class="box" /></SlAnimation>
</div>

<style>{css}</style>
</>
);
```

?> The animation will only be applied to the first child element found in `<sl-animation>`.

## Examples
Expand All @@ -42,7 +69,7 @@ This example demonstrates all of the baked-in animations and easings. Animations
<div class="controls">
<sl-select label="Animation" value="bounce"></sl-select>
<sl-select label="Easing" value="linear"></sl-select>
<sl-range min="0" max="2" step=".5" value="1"></sl-range>
<sl-input label="Playback Rate" type="number" min="0" max="2" step=".25" value="1"></sl-input>
</div>
</div>

Expand All @@ -53,7 +80,7 @@ This example demonstrates all of the baked-in animations and easings. Animations
const animation = container.querySelector('sl-animation');
const animationName = container.querySelector('.controls sl-select:nth-child(1)');
const easingName = container.querySelector('.controls sl-select:nth-child(2)');
const playbackRate = container.querySelector('sl-range');
const playbackRate = container.querySelector('sl-input[type="number"]');
const animations = getAnimationNames();
const easings = getEasingNames();
Expand All @@ -75,8 +102,7 @@ This example demonstrates all of the baked-in animations and easings. Animations
animationName.addEventListener('sl-change', () => animation.name = animationName.value);
easingName.addEventListener('sl-change', () => animation.easing = easingName.value);
playbackRate.addEventListener('sl-change', () => animation.playbackRate = playbackRate.value);
playbackRate.tooltipFormatter = val => `Playback Rate = ${val}`;
playbackRate.addEventListener('sl-input', () => animation.playbackRate = playbackRate.value);
</script>

<style>
Expand Down Expand Up @@ -134,6 +160,63 @@ Use an [Intersection Observer](https://developer.mozilla.org/en-US/docs/Web/API/
</style>
```

```jsx react
import { useEffect, useRef, useState } from 'react';
import { SlAnimation } from '@shoelace-style/shoelace/dist/react';

const css = `
.animation-scroll {
height: calc(100vh + 100px);
}
.animation-scroll .box {
display: inline-block;
width: 100px;
height: 100px;
background-color: rgb(var(--sl-color-primary-600));
}
`;

const App = () => {
const animation = useRef(null);
const box = useRef(null);

useEffect(() => {
const observer = new IntersectionObserver(
(entries) => {
if (entries[0].isIntersecting) {
animation.current.play = true;
} else {
animation.current.play = false;
animation.current.currentTime = 0;
}
}
);

if (box.current) {
observer.observe(box.current);
}
}, [box]);

return (
<>
<div class="animation-scroll">
<SlAnimation
ref={animation}
name="jackInTheBox"
duration={2000}
iterations={1}
>
<div ref={box} class="box" />
</SlAnimation>
</div>

<style>{css}</style>
</>
);
};
```

### Custom Keyframe Formats

Supply your own [keyframe formats](https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Keyframe_Formats) to build custom animations.
Expand Down Expand Up @@ -174,6 +257,50 @@ Supply your own [keyframe formats](https://developer.mozilla.org/en-US/docs/Web/
</style>
```

```jsx react
import { SlAnimation } from '@shoelace-style/shoelace/dist/react';

const css = `
.animation-keyframes .box {
width: 100px;
height: 100px;
background-color: rgb(var(--sl-color-primary-600));
}
`;

const App = () => (
<>
<div class="animation-keyframes">
<SlAnimation
easing="ease-in-out"
duration={2000}
play
keyframes={[
{
offset: 0,
easing: 'cubic-bezier(0.250, 0.460, 0.450, 0.940)',
fillMode: 'both',
transformOrigin: 'center center',
transform: 'rotate(0)'
},
{
offset: 1,
easing: 'cubic-bezier(0.250, 0.460, 0.450, 0.940)',
fillMode: 'both',
transformOrigin: 'center center',
transform: 'rotate(90deg)'
}
]}
>
<div class="box" />
</SlAnimation>
</div>

<style>{css}</style>
</>
);
```

### Playing Animations on Demand

Animations won't play until you apply the `play` attribute. You can omit it initially, then apply it on demand such as after a user interaction. In this example, the button will animate once every time the button is clicked.
Expand All @@ -196,4 +323,29 @@ Animations won't play until you apply the `play` attribute. You can omit it init
</script>
```

```jsx react
import { useState } from 'react';
import { SlAnimation, SlButton } from '@shoelace-style/shoelace/dist/react';

const App = () => {
const [play, setPlay] = useState(false);

return (
<div class="animation-form">
<SlAnimation
name="rubberBand"
duration={1000}
iterations={1}
play={play}
onSlFinish={() => setPlay(false)}
>
<SlButton type="primary" onClick={() => setPlay(true)}>
Click me
</SlButton>
</SlAnimation>
</div>
);
};
```

[component-metadata:sl-animation]

0 comments on commit da7a177

Please sign in to comment.