Skip to content

Commit

Permalink
Merge pull request #55 from drummes12/scroll-driven_animations
Browse files Browse the repository at this point in the history
Scroll driven animations
  • Loading branch information
midudev authored Mar 14, 2024
2 parents b518abb + 4021db6 commit 90912f0
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 1 deletion.
42 changes: 42 additions & 0 deletions README.es.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,48 @@ Este plugin trae varias clases de utilidad así como varias animaciones CSS list
</div>
```

### Animates Timeline

Este plugin también trae una clase de utilidad para animar elementos basados en su posición en la ventana. Puedes usar la clase `view-animate-single` o `view-animate-[animation]` para poder generar cualquier nombre a tu timeline.

```html
<div class="w-3/4 max-w-[800px] m-[0_auto]">
<h1>Content</h1>

<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Risus quis varius quam
quisque id. Et ligula ullamcorper malesuada proin libero nunc consequat
interdum varius. Elit ullamcorper dignissim cras tincidunt lobortis feugiat
vivamus at augue.
</p>

<p>
Dolor sed viverra ipsum nunc aliquet. Sed sed risus pretium quam vulputate
dignissim. Tortor aliquam nulla facilisi cras. A erat nam at lectus urna
duis convallis convallis. Nibh ipsum consequat nisl vel pretium lectus.
Sagittis aliquam malesuada bibendum arcu vitae elementum. Malesuada bibendum
arcu vitae elementum curabitur vitae nunc sed velit.
</p>

<div
class="w-72 h-52 m-[0_auto] bg-[deeppink] view-animate-[--subjectReveal] animate-zoom-in animate-range-[entry_10%_contain_25%]">
</div>

<p>
Adipiscing enim eu turpis egestas pretium aenean pharetra magna ac. Arcu
cursus vitae congue mauris rhoncus aenean vel. Sit amet cursus sit amet
dictum. Augue neque gravida in fermentum et. Gravida rutrum quisque non
tellus orci ac auctor augue mauris. Risus quis varius quam quisque id diam
vel quam elementum. Nibh praesent tristique magna sit amet purus gravida
quis. Duis ultricies lacus sed turpis tincidunt id aliquet. In egestas erat
imperdiet sed euismod nisi. Eget egestas purus viverra accumsan in nisl nisi
scelerisque. Netus et malesuada fames ac.
</p>
</div>
```
> Ejemplo extraido de [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/view-timeline)
## Contribuidores

<a href="https://github.com/midudev/tailwind-animations/graphs/contributors">
Expand Down
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,48 @@ export default {
</div>
```

### Animates Timeline

This plugin also brings a utility class to animate elements based on their position in the window. You can use the class `view-animate-single` or `view-animate-[animation]` to generate any name for your timeline.

```html
<div class="w-3/4 max-w-[800px] m-[0_auto]">
<h1>Content</h1>

<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Risus quis varius quam
quisque id. Et ligula ullamcorper malesuada proin libero nunc consequat
interdum varius. Elit ullamcorper dignissim cras tincidunt lobortis feugiat
vivamus at augue.
</p>

<p>
Dolor sed viverra ipsum nunc aliquet. Sed sed risus pretium quam vulputate
dignissim. Tortor aliquam nulla facilisi cras. A erat nam at lectus urna
duis convallis convallis. Nibh ipsum consequat nisl vel pretium lectus.
Sagittis aliquam malesuada bibendum arcu vitae elementum. Malesuada bibendum
arcu vitae elementum curabitur vitae nunc sed velit.
</p>

<div
class="w-72 h-52 m-[0_auto] bg-[deeppink] view-animate-[--subjectReveal] animate-zoom-in animate-range-[entry_10%_contain_25%]">
</div>

<p>
Adipiscing enim eu turpis egestas pretium aenean pharetra magna ac. Arcu
cursus vitae congue mauris rhoncus aenean vel. Sit amet cursus sit amet
dictum. Augue neque gravida in fermentum et. Gravida rutrum quisque non
tellus orci ac auctor augue mauris. Risus quis varius quam quisque id diam
vel quam elementum. Nibh praesent tristique magna sit amet purus gravida
quis. Duis ultricies lacus sed turpis tincidunt id aliquet. In egestas erat
imperdiet sed euismod nisi. Eget egestas purus viverra accumsan in nisl nisi
scelerisque. Netus et malesuada fames ac.
</p>
</div>
```
> Example extracted from [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/view-timeline)
## Contributors 👑

<a href="https://github.com/midudev/tailwind-animations/graphs/contributors">
Expand Down
18 changes: 17 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,29 @@ import theme from './theme.js'
const pluginCreator = api => {
const { theme, matchUtilities, addUtilities } = api

// Predefined animations in same element
const singleTimeline = value => {
let customNameTimeline = value
if (value.startsWith('var(')) customNameTimeline = value.slice(4, -1)

return customNameTimeline
}

const dynamicUtils = {
'animate-delay': { css: 'animation-delay', values: theme('animationDelay') },
'animate-duration': { css: 'animation-duration', values: theme('animationDuration') },
'animate-iteration-count': { css: 'animation-iteration-count', values: theme('animationIterationCount') },
'animate-fill-mode': { css: 'animation-fill-mode', values: theme('animationFillMode') },
'animate-bezier': { css: 'animation-timing-function', values: theme('animationCubicBezier') },
'animate-steps': { css: 'animation-timing-function', values: theme('animationSteps'), generateValue: value => `steps(${value})` }
'animate-steps': { css: 'animation-timing-function', values: theme('animationSteps'), generateValue: value => `steps(${value})` },
'animate-range': { css: 'animation-range', values: theme('animationRange'), generateValue: value => value },
timeline: { css: 'animation-timeline', values: theme('timeline'), generateValue: value => singleTimeline(value) },
'scroll-timeline': { css: 'scroll-timeline-name', values: theme('scrollTimeline'), generateValue: (value) => singleTimeline(value) },
'view-timeline': { css: 'view-timeline-name', values: theme('viewTimeline'), generateValue: (value) => singleTimeline(value) },
'scroll-timeline-axis': { css: 'scroll-timeline-axis', values: theme('scrollTimelineAxis') },
'view-timeline-axis': { css: 'view-timeline-axis', values: theme('viewTimelineAxis') },
'scroll-animate': { css: 'scroll-timeline-name', values: theme('scrollTimeline'), generateValue: (value) => `${singleTimeline(value)};\n animation-timeline: ${singleTimeline(value)}` },
'view-animate': { css: 'view-timeline-name', values: theme('viewTimeline'), generateValue: (value) => `${singleTimeline(value)};\n animation-timeline: ${singleTimeline(value)}` }
}

Object.entries(dynamicUtils).forEach(([name, { css, values, generateValue }]) => {
Expand Down
42 changes: 42 additions & 0 deletions src/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,5 +518,47 @@ export default {
'back-in': 'cubic-bezier(0.6,-0.28,0.735,0.045)',
'back-out': 'cubic-bezier(0.175,0.885,0.32,1.275)',
'back-in-out': 'cubic-bezier(0.68,-0.55,0.265,1.55)'
},
animationRange: {
normal: 'normal',
cover: 'cover',
contain: 'contain',
entry: 'entry',
exit: 'exit',
gradual: '10% 90%',
moderate: '20% 80%',
brisk: '30% 70%',
rapid: '40% 60%'
},
timeline: {
none: 'none',
auto: 'auto',
single: '--single-timeline',
scroll: 'scroll()',
view: 'view()'
},
scrollTimeline: {
single: '--single-timeline'
},
viewTimeline: {
single: '--single-timeline'
},
scrollTimelineAxis: {
block: 'block',
inline: 'inline',
x: 'x',
y: 'y'
},
viewTimelineAxis: {
block: 'block',
inline: 'inline',
x: 'x',
y: 'y'
},
scrollAnimation: {
single: '--single-timeline'
},
viewAnimation: {
single: '--single-timeline'
}
}
128 changes: 128 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,132 @@ describe('tailwindcss-animations plugins', () => {

expect(css).toMatch('.animate-play-paused{animation-play-state:paused}')
})

it('use a animation timeline none or auto', async () => {
const css = await generatePluginCSS({
content: '<div class="timeline-none">Hello</div>'
})

expect(css).toMatch('.timeline-none{animation-timeline:none}')
})

it('use a animation timeline single', async () => {
const css = await generatePluginCSS({
content: '<div class="timeline-single">Hello</div>'
})

expect(css).toMatch('.timeline-single{animation-timeline:--single-timeline}')
})

it('use a animation timeline scroll', async () => {
const css = await generatePluginCSS({
content: '<div class="timeline-scroll">Hello</div>'
})

expect(css).toMatch('.timeline-scroll{animation-timeline:scroll()}')
})

it('use a animation timeline scroll custom', async () => {
const css = await generatePluginCSS({
content: '<div class="timeline-[scroll(20%)]">Hello</div>'
})

expect(css).toMatch('.timeline-\\[scroll\\(20\\%\\)\\]{animation-timeline:scroll(20%)}')
})

it('use a animation timeline custom name', async () => {
const css = await generatePluginCSS({
content: '<div class="timeline-[--test]">Hello</div>'
})

expect(css).toMatch('.timeline-\\[--test\\]{animation-timeline:--test}')
})

it('use a scroll timeline single', async () => {
const css = await generatePluginCSS({
content: '<div class="scroll-timeline-single">Hello</div>'
})

expect(css).toMatch('.scroll-timeline-single{scroll-timeline-name:--single-timeline}')
})

it('use a view timeline single', async () => {
const css = await generatePluginCSS({
content: '<div class="view-timeline-single">Hello</div>'
})

expect(css).toMatch('.view-timeline-single{view-timeline-name:--single-timeline}')
})

it('use a view timeline custom name', async () => {
const css = await generatePluginCSS({
content: '<div class="view-timeline-[--test-view]">Hello</div>'
})

expect(css).toMatch('.view-timeline-\\[--test-view\\]{view-timeline-name:--test-view}')
})

it('use a timeline range', async () => {
const css = await generatePluginCSS({
content: '<div class="animate-range-cover">Hello</div>'
})

expect(css).toMatch('.animate-range-cover{animation-range:cover}')
})

it('use a timeline range porcentual', async () => {
const css = await generatePluginCSS({
content: '<div class="animate-range-moderate">Hello</div>'
})

expect(css).toMatch('.animate-range-moderate{animation-range:20% 80%}')
})

it('use a timeline range custom', async () => {
const css = await generatePluginCSS({
content: '<div class="animate-range-[12%_65%]">Hello</div>'
})

expect(css).toMatch('.animate-range-\\[12\\%_65\\%\\]{animation-range:12% 65%}')
})

it('use a scroll timeline axis', async () => {
const css = await generatePluginCSS({
content: '<div class="scroll-timeline-axis-block">Hello</div>'
})

expect(css).toMatch('.scroll-timeline-axis-block{scroll-timeline-axis:block}')
})

it('use a view timeline axis', async () => {
const css = await generatePluginCSS({
content: '<div class="view-timeline-axis-y">Hello</div>'
})

expect(css).toMatch('.view-timeline-axis-y{view-timeline-axis:y}')
})

it('use view animation timeline in same element', async () => {
const css = await generatePluginCSS({
content: '<div class="view-animate-single">Hello</div>'
})

expect(css).toMatch('.view-animate-single{view-timeline-name:--single-timeline; animation-timeline: --single-timeline}')
})

it('use scroll animation timeline in same element', async () => {
const css = await generatePluginCSS({
content: '<div class="scroll-animate-single">Hello</div>'
})

expect(css).toMatch('.scroll-animate-single{scroll-timeline-name:--single-timeline; animation-timeline: --single-timeline}')
})

it('use scroll animation timeline in same element with custom name', async () => {
const css = await generatePluginCSS({
content: '<div class="scroll-animate-[--test-timeline-scroll]">Hello</div>'
})

expect(css).toMatch('.scroll-animate-\\[--test-timeline-scroll\\]{scroll-timeline-name:--test-timeline-scroll; animation-timeline: --test-timeline-scroll}')
})
})

0 comments on commit 90912f0

Please sign in to comment.