Last active
February 16, 2022 09:10
-
-
Save jdnichollsc/cecb152ecdd41e2a3733a3dfe03f0ce3 to your computer and use it in GitHub Desktop.
Animatable Components using Higher Order Component (HOC) with StencilJS π - https://proyecto26.github.io/animatable-component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<animatable-component | |
autoplay | |
easing="ease-in-out" | |
duration="800" | |
delay="300" | |
animation="zoomIn" | |
iterations="Infinity" | |
direction="alternate" | |
> | |
<h1>Hello World</h1> | |
</animatable-component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, h, Element, Prop, Host, Event, EventEmitter } from '@stencil/core'; | |
@Component({ | |
tag: 'animatable-component' | |
}) | |
export class AnimatableComponent { | |
@Element() el!: HTMLElement; | |
@Prop() keyFrames: Keyframe[] | |
@Prop() options: KeyframeAnimationOptions | |
@Event() finish: EventEmitter; | |
animateComponent() { | |
const element = this.el.querySelectorAll(':first-child')[0]; | |
const animation = element.animate(this.keyFrames, this.options) | |
animation.play(); | |
animation.onfinish = () => this.finish.emit(element) | |
} | |
componentWillLoad() { | |
this.animateComponent() | |
} | |
componentDidUpdate() { | |
this.animateComponent() | |
} | |
render() { | |
return <Host> | |
<slot /> | |
</Host> | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, h } from '@stencil/core'; | |
import { | |
SendMessageButton, | |
createAnimatableComponent, | |
keyFramesSendMessage, | |
optionsSendMessage, | |
} from './utils' | |
const AnimatableSendMessageButton = createAnimatableComponent(SendMessageButton) | |
@Component({ | |
tag: 'my-animated-component' | |
}) | |
export class MyAnimatedComponent { | |
render() { | |
return ( | |
<AnimatableSendMessageButton | |
keyFrames={keyFramesSendMessage} | |
options={optionsSendMessage} | |
onClick={() => alert('Eureka')} | |
/> | |
) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function createAnimatableComponent ( | |
WrappedComponent: FunctionalComponent | |
) { | |
return (props) => { | |
const { keyFrames, options, onFinish, ...rest } = props | |
return ( | |
<animatable-component | |
keyFrames={keyFrames} | |
options={options} | |
onFinish={onFinish} | |
> | |
<WrappedComponent {...rest} /> | |
</animatable-component> | |
) | |
} | |
}; | |
export const SendMessageButton = (props) => { | |
return ( | |
<ion-fab-button {...props}> | |
<ion-icon name='send' /> | |
</ion-fab-button> | |
) | |
} | |
export const keyFramesSendMessage: Keyframe[] = [ | |
{ | |
opacity: '0', | |
transform: 'rotate(0deg)' | |
}, | |
{ | |
opacity: '1', | |
transform: 'rotate(360deg)' | |
} | |
] | |
export const optionsSendMessage: KeyframeAnimationOptions = { | |
duration: 500, | |
easing: 'ease-in-out' | |
} |
I made a WebComponent using HOC :) https://github.com/proyecto26/animatable-component/blob/master/src/hocs/index.tsx
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That looks beautiful, gotta try!