Created
July 11, 2023 20:07
-
-
Save wysRocket/a1e258931986957fe02399a626cdc5d2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| <!DOCTYPE html> | |
| <head> | |
| <script type="module" src="./motion-carousel.js"></script> | |
| <style> | |
| body { | |
| margin: 0; | |
| height: 100vh; | |
| width: 100vw; | |
| font-family: sans-serif; | |
| display: flex; | |
| justify-content: center; | |
| align-items: center; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <motion-carousel></motion-carousel> | |
| </body> |
This file contains hidden or 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 {LitElement, html} from 'lit'; | |
| import {customElement, property} from 'lit/decorators.js'; | |
| import {animate} from '@lit-labs/motion'; | |
| import {styleMap} from 'lit/directives/style-map.js'; | |
| import {styles} from './styles.js'; | |
| @customElement('motion-carousel') | |
| export class MotionCarousel extends LitElement { | |
| static styles = styles; | |
| @property({type: Number}) selected = 0; | |
| data = Array(7).fill(null, 0); | |
| render() { | |
| return html` | |
| <section class="container"> | |
| ${this.data.map((_v, i) => { | |
| const count = this.data.length; | |
| const center = Math.trunc(count / 2); | |
| const order = (count + center + i - this.selected) % count; | |
| const zIndex = order === 0 || order === count - 1 ? -1 : 1; | |
| const fraction = i / this.data.length; | |
| return html`<div | |
| @click=${order < center ? this.dec : this.inc} | |
| style=${styleMap({ | |
| order: String(order), | |
| zIndex: String(zIndex), | |
| background: `hsl( | |
| ${Math.trunc(360 * fraction)}, | |
| ${20 + Math.trunc(60 * fraction)}%, | |
| ${30 + Math.trunc(30 * fraction)}%)`, | |
| })} | |
| class="card" | |
| ${animate()} | |
| > | |
| ${i} | |
| </div>`; | |
| })} | |
| </section> | |
| `; | |
| } | |
| shift(i: number) { | |
| const last = this.data.length - 1; | |
| return i > last ? 0 : i < 0 ? last : i; | |
| } | |
| dec() { | |
| this.selected = this.shift(this.selected - 1); | |
| } | |
| inc() { | |
| this.selected = this.shift(this.selected + 1); | |
| } | |
| } |
This file contains hidden or 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
| { | |
| "dependencies": { | |
| "lit": "^2.7.4", | |
| "@lit-labs/motion": "^1.0.3" | |
| } | |
| } |
This file contains hidden or 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 {css} from 'lit'; | |
| export const styles = [ | |
| css` | |
| .container { | |
| display: flex; | |
| align-items: center; | |
| justify-content: center; | |
| --size: 50vw; | |
| width: calc(var(--size) + 80px); | |
| gap: 8px; | |
| border-radius: 8px; | |
| padding: 8px; | |
| overflow: hidden; | |
| border: 4px solid #002071; | |
| background: #e1e2e1; | |
| } | |
| .card { | |
| background: #e1e2e1; | |
| border-radius: 8px; | |
| border: 1px solid #002071; | |
| height: var(--size); | |
| min-width: var(--size); | |
| padding: 8px; | |
| will-change: transform; | |
| display: flex; | |
| align-items: center; | |
| justify-content: center; | |
| color: white; | |
| font-size: var(--size); | |
| cursor: pointer; | |
| user-select: none; | |
| } | |
| `, | |
| ]; |
This file contains hidden or 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
| { | |
| "files": { | |
| "motion-carousel.ts": { | |
| "position": 0 | |
| }, | |
| "styles.ts": { | |
| "position": 1 | |
| }, | |
| "index.html": { | |
| "position": 2 | |
| }, | |
| "package.json": { | |
| "position": 3, | |
| "hidden": true | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment