Skip to content

Commit

Permalink
feat: react motion
Browse files Browse the repository at this point in the history
  • Loading branch information
wkylin committed Dec 24, 2024
1 parent 7c0f009 commit 08a7ffc
Show file tree
Hide file tree
Showing 2 changed files with 214 additions and 5 deletions.
34 changes: 34 additions & 0 deletions src/pages/motion/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.progress {
position: sticky;
top: 0;
width: 80px;
height: 80px;
margin: 0;
padding: 0;
}

.progress svg {
transform: translateX(-100px) rotate(-90deg);
}

circle {
stroke-width: 5%;
fill: none;
}

.bg {
stroke: #aaa;
opacity: 0.2;
}

.progress .indicator {
stroke: #f00;
}

.item {
width: 200px;
height: 300px;
border: 2px dotted #aaa;
margin-left: 300px;
position: relative;
}
185 changes: 180 additions & 5 deletions src/pages/motion/index.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import React, { useEffect, useState, useRef } from 'react'

Check warning on line 1 in src/pages/motion/index.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Unused import

Unused import specifier useEffect

Check failure on line 1 in src/pages/motion/index.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

ESLint

ESLint: Install the 'eslint' package
import { motion, useAnimationControls, LayoutGroup, useScroll } from 'motion/react'
import {
motion,
useAnimationControls,
LayoutGroup,
useScroll,
useMotionValueEvent,
useSpring,
useTransform,
} from 'motion/react'
import FixTabPanel from '@stateless/FixTabPanel'
import { is } from '@react-spring/shared'

import './index.css'

const animations = {
show: {
Expand Down Expand Up @@ -90,12 +99,39 @@ const ParallaxVert = () => {
const [listLabel, setListLabel] = useState(['JavaScript', 'html', 'css', 'webAssembly'])
const [isBig, setBigState] = useState(false)
const [leftState, setLeftState] = useState(false)

const scrollRef = useRef(null)

const { scrollYProgress } = useScroll({
const { scrollYProgress, scrollY } = useScroll({
container: scrollRef,
})

const x = useTransform(scrollY, (value) => {
return (value * 1) / 5
})

const ref = useRef(null)
const { scrollYProgress: scrollAa } = useScroll({
container: scrollRef,
target: ref,
offset: ['end end', 'start start'],
})

const scaleX = useSpring(scrollYProgress, {
stiffness: 100,
damping: 30,
restDelta: 0.001,
})

const [scrollDirection, setScrollDirection] = useState('down')

useMotionValueEvent(scrollY, 'change', (current) => {
const diff = current - scrollY.getPrevious()
setScrollDirection(diff > 0 ? 'down' : 'up')
})

const rotate = useTransform(scrollY, [0, 100], [0, 360], { clamp: false })

return (
<FixTabPanel ref={scrollRef}>
<h2>Hi, Motion</h2>
Expand All @@ -108,7 +144,8 @@ const ParallaxVert = () => {
height: 3,
backgroundColor: '#aaa',
borderRadius: '3px',
scaleX: scrollYProgress,
// scaleX: scrollYProgress,
scaleX: scaleX,
}}
></motion.div>
<motion.div

Check notice on line 151 in src/pages/motion/index.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Unresolved JSX component

Unresolved component motion.div
Expand Down Expand Up @@ -492,7 +529,7 @@ const ParallaxVert = () => {
borderRadius: '60px',
display: 'flex',
alignItems: 'center',
justifyContent: buttonRight ? 'flex-end' : 'flex-start', // 这里改变 <-
justifyContent: buttonRight ? 'flex-end' : 'flex-start',
padding: 10,
}}
onTap={() => {
Expand All @@ -510,6 +547,28 @@ const ParallaxVert = () => {
></motion.div>
</motion.div>

<section style={{ fontSize: 30 }}>scrollDirection: {scrollDirection}</section>
<motion.div

Check notice on line 551 in src/pages/motion/index.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Unresolved JSX component

Unresolved component motion.div
style={{
width: 200,
height: 100,
backgroundColor: '#aaa',
borderRadius: '20px',
// scaleX: scrollYProgress,
x,
rotate,
}}
></motion.div>
<motion.div

Check notice on line 562 in src/pages/motion/index.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Unresolved JSX component

Unresolved component motion.div
style={{
width: 200,
height: 100,
backgroundColor: '#aaa',
borderRadius: '20px',
scaleX,
}}
></motion.div>

<div
style={{
display: 'flex',
Expand Down Expand Up @@ -777,8 +836,124 @@ const ParallaxVert = () => {
<motion.span>黑点和文字都未设置layout</motion.span>

Check notice on line 836 in src/pages/motion/index.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Unresolved JSX component

Unresolved component motion.span
</div>
</div>
<section>
<section>
<div ref={ref} className="item">
<figure className="progress">
<svg id="progress" width="75" height="75" viewBox="0 0 100 100">
<circle cx="50" cy="50" r="30" pathLength="1" className="bg" />
<motion.circle

Check notice on line 845 in src/pages/motion/index.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Unresolved JSX component

Unresolved component motion.circle
cx="50"
cy="50"
r="30"
pathLength="1"
className="indicator"
style={{ pathLength: scrollAa }}
/>
</svg>
</figure>
</div>
</section>
<section>
<div ref={ref} className="item">
<figure className="progress">
<svg id="progress" width="75" height="75" viewBox="0 0 100 100">
<circle cx="50" cy="50" r="30" pathLength="1" className="bg" />
<motion.circle

Check notice on line 862 in src/pages/motion/index.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Unresolved JSX component

Unresolved component motion.circle
cx="50"
cy="50"
r="30"
pathLength="1"
className="indicator"
style={{ pathLength: scrollAa }}
/>
</svg>
</figure>
</div>
</section>
<section>
<div ref={ref} className="item">
<figure className="progress">
<svg id="progress" width="75" height="75" viewBox="0 0 100 100">
<circle cx="50" cy="50" r="30" pathLength="1" className="bg" />
<motion.circle

Check notice on line 879 in src/pages/motion/index.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Unresolved JSX component

Unresolved component motion.circle
cx="50"
cy="50"
r="30"
pathLength="1"
className="indicator"
style={{ pathLength: scrollAa }}
/>
</svg>
</figure>
</div>
</section>
<section>
<div ref={ref} className="item">
<figure className="progress">
<svg id="progress" width="75" height="75" viewBox="0 0 100 100">
<circle cx="50" cy="50" r="30" pathLength="1" className="bg" />
<motion.circle

Check notice on line 896 in src/pages/motion/index.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Unresolved JSX component

Unresolved component motion.circle
cx="50"
cy="50"
r="30"
pathLength="1"
className="indicator"
style={{ pathLength: scrollAa }}
/>
</svg>
</figure>
</div>
</section>
<section>
<div ref={ref} className="item">
<figure className="progress">
<svg id="progress" width="75" height="75" viewBox="0 0 100 100">
<circle cx="50" cy="50" r="30" pathLength="1" className="bg" />
<motion.circle

Check notice on line 913 in src/pages/motion/index.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Unresolved JSX component

Unresolved component motion.circle
cx="50"
cy="50"
r="30"
pathLength="1"
className="indicator"
style={{ pathLength: scrollAa }}
/>
</svg>
</figure>
</div>
</section>
</section>
</FixTabPanel>
)
}

export default ParallaxVert

function Item({ scrollRef }) {

Check warning on line 932 in src/pages/motion/index.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Unused local symbol

Unused function Item
const ref = useRef(null)
const { scrollYProgress } = useScroll({
target: ref,
offset: ['end end', 'start start'],
container: scrollRef,
})

return (
<section>
<div ref={ref} className="item">
<figure className="progress">
<svg id="progress" width="75" height="75" viewBox="0 0 100 100">
<circle cx="50" cy="50" r="30" pathLength="1" className="bg" />
<motion.circle

Check notice on line 946 in src/pages/motion/index.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Unresolved JSX component

Unresolved component motion.circle
cx="50"
cy="50"
r="30"
pathLength="1"
className="indicator"
style={{ pathLength: scrollYProgress }}
/>
</svg>
</figure>
</div>
</section>
)
}

0 comments on commit 08a7ffc

Please sign in to comment.