Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enter component #35

Merged
merged 6 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions components/composition/Enter/Enter.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
@keyframes fade {
from {
opacity: 0;
}

to {
opacity: 1;
}
}

@keyframes fade-in-up {
from {
opacity: 0;
transform: translate(0, 1rem);
}

to {
opacity: 1;
transform: translate(0, 0);
}
}

@keyframes boing {
from {
opacity: 0;
transform: scale(0.9);
}

to {
opacity: 1;
transform: scale(1);
}
}

diamond-enter {
animation-duration: var(--diamond-transition-enter-duration);
animation-fill-mode: forwards;
animation-iteration-count: 1;
animation-name: var(--diamond-transition-enter-animation);
animation-timing-function: var(--diamond-transition-enter-timing);
display: block;
opacity: 0;

&[delay] {
animation-delay: var(--diamond-transition-enter-delay);
}

&[type='boing'] {
animation-timing-function: var(--diamond-transition-enter-timing-boing);
}

&[fill] {
display: grid;
}
}
53 changes: 53 additions & 0 deletions components/composition/Enter/Enter.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { StoryObj } from '@storybook/web-components';
import { html } from 'lit';

import './Enter';

export default {
component: 'diamond-enter',
argTypes: {
type: {
control: {
type: 'radio',
},
options: ['fade', 'fade-in-up', 'boing'],
},
},
};

export const Enter: StoryObj = {
render: (args) => html`
<diamond-enter
type="${args.type}"
?enter-on-scroll="${args.enterOnScroll}"
delay="${args.delay}"
>
<div style="background-color: #eee; height: 400px;"></div>
</diamond-enter>
`,
};

Enter.args = {
type: 'fade',
enterOnScroll: false,
delay: '0.5s',
};

export const OnScroll: StoryObj = {
render: (args) => html`
<div style="height: 120vh;">Scroll down</div>
<diamond-enter
type="${args.type}"
?enter-on-scroll="${args.enterOnScroll}"
delay="${args.delay}"
>
<div style="background-color: #eee; height: 400px;"></div>
</diamond-enter>
`,
};

OnScroll.args = {
type: 'boing',
enterOnScroll: true,
delay: '0s',
};
76 changes: 76 additions & 0 deletions components/composition/Enter/Enter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { LitElement, html } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';

import { JSXCustomElement } from '../../../types/jsx-custom-element';

export interface EnterAttributes {
type?: 'fade' | 'fade-in-up' | 'boing';
enterOnScroll?: boolean;
delay?: string;
/**
* Applies display grid so that children fill the height and width of the component
*/
fill?: boolean;
DanWebb marked this conversation as resolved.
Show resolved Hide resolved
}

@customElement('diamond-enter')
export class Enter extends LitElement {
observer?: IntersectionObserver;
@state() inView: boolean = false;
@property({ reflect: true }) type?: 'fade' | 'fade-in-up' | 'boing' = 'fade';
@property({ reflect: true, type: Boolean, attribute: 'enter-on-scroll' })
enterOnScroll?: boolean = false;
@property({ reflect: true }) delay?: string = '0s';
@property({ reflect: true, type: Boolean }) fill: boolean = false;

connectedCallback(): void {
super.connectedCallback();
if (this.enterOnScroll) {
this.observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
this.inView = true;
this.observer?.unobserve(this);
}
});
},
{ threshold: 0 },
);
this.observer.observe(this);
}
}

disconnectedCallback(): void {
super.disconnectedCallback();
this.observer?.disconnect();
}

render() {
const type = this.inView || !this.enterOnScroll ? this.type : '';

return html`
<style>
:host {
--diamond-transition-enter-delay: ${this.delay};
--diamond-transition-enter-animation: ${type};
}
</style>
<slot></slot>
`;
}
}

declare global {
interface HTMLElementTagNameMap {
'diamond-enter': EnterAttributes;
}
}

declare module 'react' {
namespace JSX {
interface IntrinsicElements {
'diamond-enter': EnterAttributes & JSXCustomElement;
}
}
}
20 changes: 20 additions & 0 deletions styles/tokens/transition.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@

--diamond-transition-enter-duration: 0.5s;
--diamond-transition-enter-timing: ease-in-out;
--diamond-transition-enter-timing-boing: linear(
0,
0.06,
0.25 18%,
1 36%,
0.81,
0.75,
0.81,
1,
0.94,
1,
1
);
--diamond-transition-enter: var(--diamond-transition-enter-duration)
var(--diamond-transition-enter-timing);
}

@media (prefers-reduced-motion) {
:root {
--diamond-transition-duration: 0s;
--diamond-transition-enter-duration: 0s;
}
}
Loading