Skip to content

Commit

Permalink
Merge branch 'main' of github.com:midudev/tailwind-animations
Browse files Browse the repository at this point in the history
  • Loading branch information
midudev committed Sep 2, 2024
2 parents f87fd16 + 3e493df commit 2799ed9
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 6 deletions.
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
57 changes: 55 additions & 2 deletions src/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ export default {
'impulse-rotation-right': 'impulse-rotation-right 1s ease-in-out both',
'impulse-rotation-left': 'impulse-rotation-left 1s ease-in-out both',
dancing: 'dancing 1s ease-in-out both',
pulse: 'pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite'
pulse: 'pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite',
jelly: `jelly 1s ease-out forwards`,
},
keyframes: {
'fade-in': {
Expand Down Expand Up @@ -437,7 +438,17 @@ export default {
'50%': { transform: 'skew(40deg)' },
'75%': { transform: 'skew(-40deg)' },
'100%': { transform: 'skew(0deg)' }
}
},
jelly: {
'0%': { transform: 'scale(1, 1)' },
'20%': { transform: 'scale(1.25, 0.75)' },
'40%': { transform: 'scale(0.75, 1.25)' },
'60%': { transform: 'scale(1.15, 0.85)' },
'75%': { transform: 'scale(0.95, 1.05)' },
'85%': { transform: 'scale(1.05, 0.95)' },
'92%': { transform: 'scale(1, 1.02)' },
'100%': { transform: 'scale(1, 1)' },
},
},
animationDelay: {
none: '0ms',
Expand Down Expand Up @@ -518,5 +529,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}')
})
})
3 changes: 2 additions & 1 deletion web/src/components/ToggleTheme.astro
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const THEMES = ["Light", "Dark", "System"]
</button>
<div
id="themes-menu"
class="absolute opacity-0 scale-80 top-8 right-0 text-sm p-1 min-w-[8rem] rounded-md border border-gray-100 bg-white/90 dark:bg-zinc-900/70 dark:border-gray-500/20 shadow-[0_3px_10px_rgb(0,0,0,0.2)] backdrop-blur-md z-50"
class="absolute opacity-0 hidden scale-80 top-8 right-0 text-sm p-1 min-w-[8rem] rounded-md border border-gray-100 bg-white/90 dark:bg-zinc-900/70 dark:border-gray-500/20 shadow-[0_3px_10px_rgb(0,0,0,0.2)] backdrop-blur-md z-50"
>
<ul>
{
Expand All @@ -39,6 +39,7 @@ const THEMES = ["Light", "Dark", "System"]
<style>
#themes-menu.open {
animation: scale-up-center 0.15s cubic-bezier(0.25, 0.46, 0.45, 0.94) both;
display: block;
}

@keyframes scale-up-center {
Expand Down
17 changes: 15 additions & 2 deletions web/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ export default {
<label class='relative inline-flex cursor-pointer items-center'>
<input type='checkbox' value='' class='peer sr-only' id='animate' />
<div
class="rtl:peer-checked:after:-translate-x- peer h-8 w-16 rounded-full bg-gray-200 after:absolute after:start-[2px] after:top-[2px] after:size-7 after:rounded-full after:border after:border-gray-300 after:bg-white after:transition-all after:content-[''] peer-checked:bg-zinc-600 peer-checked:after:translate-x-[40px] peer-checked:after:border-white peer-focus:outline-none dark:bg-zinc-300"
class="rtl:peer-checked:after:-translate-x peer h-8 w-16 rounded-full bg-gray-200 after:absolute after:start-[2px] after:top-[2px] after:size-7 after:rounded-full after:border after:border-gray-300 after:bg-white after:transition-all after:content-[''] peer-checked:bg-zinc-600 peer-checked:after:translate-x-[32px] peer-checked:after:border-white peer-focus:outline-none dark:bg-zinc-300"
>
</div>
<span class='ms-3 text-xl font-medium text-gray-900 dark:text-gray-100'>Animate all</span>
</label>
</section>
<section class='mb-6 grid grid-flow-col gap-6 columns-4 items-center justify-center'>
<section class='mb-6 grid md:grid-flow-col grid-flow-row gap-6 columns-4 items-center justify-center sticky top-0 z-10 pt-2' id="option-inputs">
<label for="duration" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">
Duration
<select name="duration" id="duration" class="mt-2 bg-gray-50 min-w-36 border border-gray-300 text-gray-900 text-sm rounded-lg block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white">
Expand Down Expand Up @@ -180,6 +180,7 @@ export default {
const $duration: HTMLSelectElement = document.querySelector('#duration')!
const $steps: HTMLSelectElement = document.querySelector('#steps')!
const $delay: HTMLSelectElement = document.querySelector('#delay')!
const $optionInputs: HTMLElement = document.getElementById('option-inputs')!

// functions to use as events handlers
// created to add and remove the events depending on the toggle
Expand Down Expand Up @@ -279,4 +280,16 @@ export default {
}
})
})

window.addEventListener('scroll', () => {
const isSticky = $optionInputs.getBoundingClientRect().top < 20

$optionInputs.classList.toggle('is-sticky', isSticky)
})
</script>

<style>
.is-sticky {
@apply bg-zinc-900/65 backdrop-blur-sm;
}
</style>

0 comments on commit 2799ed9

Please sign in to comment.