Skip to content

Instantly share code, notes, and snippets.

@shshaw
Created May 24, 2021 22:31
Show Gist options
  • Save shshaw/f7c919dd99688aee2c7ce1c68082c981 to your computer and use it in GitHub Desktop.
Save shshaw/f7c919dd99688aee2c7ce1c68082c981 to your computer and use it in GitHub Desktop.
React Circular Slider | @keyframers 4.2
<!--
1. Slide Navigation
2. Slide Animations with CSS Variables
3. Stat Placement on a circle
-->
<div id="root"></div>
<script>
const slides = [{
image: "https://images.unsplash.com/photo-1621061415651-2b7fa415360b?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTYyMTg4NzkwOQ&ixlib=rb-1.2.1&q=80&w=800&h=800&fit=facearea&facepad=5",
name: "Lamar J.",
quote: "Donec nunc ligula, vulputate quis mollis eu, interdum quis libero. Donec nulla ante, facilisis sit amet.",
stats: [{
label: "Time",
value: "~4 years"
},
{
label: "Visits",
value: "11"
},
{
label: "Funds",
value: "$2,201"
},
{
label: "Outcome",
value: "Found housing, employment"
},
]
},
{
image: "https://images.unsplash.com/photo-1621188300102-afe44791fe3b?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTYyMTg4ODA2OA&ixlib=rb-1.2.1&q=80&w=800&h=800&fit=facearea&facepad=5",
name: "Nicole S.",
quote: "Aliquam eu iaculis mauris. Etiam arcu lectus, imperdiet sit amet volutpat vitae.",
stats: [{
label: "Time",
value: "10 years"
},
{
label: "Visits",
value: "14"
},
{
label: "Funds",
value: "$925"
},
{
label: "Outcome",
value: "Improved health, found employment"
},
]
},
{
image: "https://images.unsplash.com/photo-1551861568-c0afa6653bc1?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTYyMTg4ODU5Ng&ixlib=rb-1.2.1&q=80&w=800&h=800&fit=facearea&facepad=5",
name: "Pamela W.",
quote: "Maecenas vestibulum, nulla sed tincidunt viverra, lorem turpis aliquam urna, ut pretium orci purus consequat augue. Etiam a enim orci, vitae pulvinar odio.",
stats: [{
label: "Time",
value: "3 years"
},
{
label: "Visits",
value: "6"
},
{
label: "Funds",
value: "$325"
},
{
label: "Outcome",
value: "Improved health, found housing"
}
]
},
{
image: "https://images.unsplash.com/photo-1603208636525-8825c33ed34b?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTYyMTg4ODIzOQ&ixlib=rb-1.2.1&q=80&w=800&h=800&fit=facearea&facepad=5",
name: "Will K.",
quote: "Aliquam tempus odio adipiscing diam pellentesque rhoncus. Curabitur a bibendum est.",
stats: [{
label: "Time",
value: "~3 years"
},
{
label: "Visits",
value: "6"
},
{
label: "Funds",
value: "$240/mo"
},
{
label: "Outcome",
value: "Improved health, employment"
}
]
},
{
image: "https://images.unsplash.com/photo-1593339915213-a94e3b1a0f64?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTYyMTg4ODM3OA&ixlib=rb-1.2.1&q=80&w=800&h=800&fit=facearea&facepad=5",
name: "Barb R.",
quote: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent bibendum, lorem vel tincidunt imperdiet.",
stats: [{
label: "Time",
value: "5 years"
},
{
label: "Visits",
value: "3"
},
{
label: "Funds",
value: "$240/mo"
},
{
label: "Outcome",
value: "Found housing, improved health"
}
]
},
{
image: "https://images.unsplash.com/photo-1620506117452-99cd6b6dcd3e?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTYyMTg4ODg0OQ&ixlib=rb-1.2.1&q=80&w=800&h=800&fit=facearea&facepad=5",
name: "Chuck C.",
quote: "Etiam a enim orci, vitae pulvinar odio. In elit urna, tincidunt a pellentesque et, scelerisque at nibh. Sed nec est sapien.",
stats: [{
label: "Time",
value: "6 years"
},
{
label: "Visits",
value: "9"
},
{
label: "Funds",
value: "$270/mo"
},
{
label: "Outcome",
value: "Improved health, housing"
}
]
}
];
</script>

React Circular Slider | @keyframers 4.2

David Khourshid & Stephen Shaw build a fancy circular React slider from scratch using CSS Variables!

🎥 Video: https://youtu.be/sFJa1ThZReA 💡 Inspiration: https://dribbble.com/shots/15713715-Samaritan-Stories-of-Impact-slider 💻 Code: (coming soon...)

Like what we're doing? Support @keyframers so we can keep live coding awesome animations!

A Pen by @keyframers on CodePen.

License.

import React, { useState, useMemo } from "https://cdn.skypack.dev/react";
import ReactDOM from "https://cdn.skypack.dev/react-dom";
function Slide({ slide, previous }) {
return (
<div
className="slide"
style={{
"--stats": slide.stats.length
}}
data-previous={previous || undefined}
>
<div className="slide-name">{slide.name}</div>
<img className="slide-image" src={slide.image} />
{slide.quote && <div className="slide-quote">{slide.quote}</div>}
{slide.stats.map((stat, i) => {
return (
<div
className="slide-stat"
key={i}
style={{
"--i": i
}}
>
<div className="stat-label">{stat.label}</div>
<div className="stat-value">{stat.value}</div>
</div>
);
})}
</div>
);
}
function App() {
const [slideIndex, setSlideIndex] = useState(0);
const [prevSlideIndex, setPrevSlideIndex] = useState(null);
const activeSlide = useMemo(() => slides[slideIndex], [slideIndex]);
const prevSlide = useMemo(() => slides[prevSlideIndex], [prevSlideIndex]);
function nextSlide() {
setPrevSlideIndex(slideIndex);
setSlideIndex((slideIndex + 1) % slides.length);
}
return (
<div className="app">
<svg
viewBox="0 0 100 100"
className="dashes"
fill="none"
stroke="var(--color-purple)"
stroke-dasharray="2 4 4 3 2 3 8 2 3 5"
>
<circle r="45" cx="50" cy="50" />
</svg>
<Slide slide={activeSlide} key={slideIndex} />
{prevSlide && (
<Slide slide={prevSlide} key={prevSlideIndex + "prev"} previous />
)}
<button
className="button -next"
onClick={() => {
nextSlide();
}}
>
Next
<br />
Story
</button>
</div>
);
}
ReactDOM.render(<App />, document.querySelector("#root"));
@import url("https://fonts.googleapis.com/css2?family=Raleway:wght@300;500&display=swap");
* {
position: relative;
box-sizing: border-box;
}
:root {
--duration: 0.8s;
--color-purple: #828dd6;
}
html,
body {
height: 100%;
padding: 0;
margin: 0;
font-family: Raleway, sans-serif;
overflow: hidden;
}
body {
min-height: 100%;
display: grid;
place-items: center;
background: #23274a;
color: white;
}
img {
max-width: 100%;
}
// ......
.app {
display: grid;
grid-template-columns: 10% 1fr 10%;
grid-template-rows: 1fr;
> .dashes {
margin: -1rem;
stroke: var(--color-purple);
stroke-linecap: round;
stroke-width: 0.6;
animation: rotate 15s linear infinite;
@keyframes rotate {
to {
transform: rotate(1turn);
}
}
}
> .dashes,
> .slide {
grid-column: 1 / -1;
grid-row: 1 / -1;
}
> .button.-next {
grid-column: -2 / -1;
grid-row: 1 / -1;
align-self: center;
}
}
.slide {
padding: 2rem;
--radius: 35vw;
width: calc(var(--radius) * 2);
height: calc(var(--radius) * 2);
display: grid;
grid-template-areas: "layer";
place-items: center;
--default-animation: fade-in;
--name-animation: name-enter;
--image-animation: image-enter;
--stat-animation: stat-enter;
--quote-animation: quote-enter;
--delay: var(--duration);
> * {
grid-area: layer;
animation: var(--default-animation) var(--duration) ease-in-out var(--delay)
both;
}
> .slide-name {
align-self: end;
text-align: center;
z-index: 2;
}
&[data-previous] {
z-index: 2;
--default-animation: fade-out;
--name-animation: name-exit;
--image-animation: image-exit;
--stat-animation: stat-exit;
--quote-animation: quote-exit;
--delay: 0s;
}
}
@keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@keyframes fade-out {
from {
opacity: 1;
}
to {
opacity: 0;
}
}
.slide-name {
font-size: 5rem;
--default-animation: var(--name-animation);
}
.slide-image {
--default-animation: var(--image-animation);
}
.slide-stat {
--default-animation: var(--stat-animation);
}
.slide-quote {
--default-animation: var(--quote-animation);
}
@keyframes name-enter {
from {
transform: translateX(100%);
opacity: 0;
}
to {
transform: none;
opacity: 1;
}
}
@keyframes name-exit {
from {
transform: none;
opacity: 1;
}
to {
transform: translateX(-100%);
opacity: 0;
}
}
@keyframes image-enter {
from {
transform: scale(1.2);
opacity: 0;
}
to {
transform: none;
opacity: 1;
}
}
@keyframes image-exit {
from {
transform: none;
opacity: 1;
}
to {
transform: scale(0.7);
opacity: 0;
}
}
@property --r-offset {
syntax: "<angle>";
inherits: false;
initial-value: 0turn;
}
@keyframes stat-enter {
from {
--r-offset: -0.3turn;
opacity: 0;
}
to {
opacity: 1;
}
}
@keyframes stat-exit {
from {
opacity: 1;
}
to {
--r-offset: -0.5turn;
opacity: 0;
}
}
@keyframes quote-enter {
from {
transform: translate(50%, -100%);
opacity: 0;
}
to {
transform: translate(100%, -200%);
opacity: 1;
}
}
@keyframes quote-exit {
from {
transform: translate(100%, -200%);
opacity: 1;
}
to {
transform: translate(50%, -100%);
opacity: 0;
}
}
.slide-image {
border-radius: 100vw;
box-shadow: 0 0.5rem 2rem #0007;
}
.slide-stat,
.slide-quote {
--transform: rotate(var(--r)) translate(var(--radius))
rotate(calc(-1 * var(--r)));
transform: var(--transform);
}
.slide-quote {
top: 0;
left: 0;
box-shadow: 0 1rem 2rem #000a;
min-width: 15rem;
}
.slide-stat {
display: inline-block;
color: #fff;
max-width: 12em;
z-index: 3;
--d: calc(var(--i) / var(--stats));
--r-offset: -0.4turn;
--r: calc(var(--r-offset) + (-0.3turn * var(--d)));
&:last-of-type {
--r: calc(var(--r-offset) + 0.45turn);
}
}
.slide-quote {
background: linear-gradient(45deg, #23274a, lighten(#23274a, 10%));
padding: 1rem;
font-size: 0.8rem;
line-height: 1.6;
max-width: 12em;
--r: 0.9turn;
}
.stat-label {
text-transform: uppercase;
font-size: 0.75rem;
line-height: 1.5;
opacity: 0.7;
letter-spacing: 0.5ch;
white-space: nowrap;
font-weight: bold;
}
.stat-value {
font-size: 1.5rem;
}
.button {
z-index: 999999;
text-transform: uppercase;
appearance: none;
background: white;
color: #1b1d3a;
font-weight: bold;
height: 12vmin;
width: 12vmin;
border-radius: 50%;
border: none;
opacity: 0.3;
transition: opacity 0.3s ease;
line-height: 1.5;
&:hover {
cursor: pointer;
opacity: 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment