Skip to content

Commit

Permalink
SplitText
Browse files Browse the repository at this point in the history
  • Loading branch information
clementroche committed Oct 11, 2023
1 parent 2290460 commit 090b555
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 53 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ yarn-error.log*
.vercel

.eslintcache

.npmrc
1 change: 0 additions & 1 deletion .npmrc

This file was deleted.

60 changes: 60 additions & 0 deletions components/split-text/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { useRect } from '@studio-freight/hamo'
import { gsap } from 'gsap'
import { SplitText as GSAPSplitText } from 'gsap/dist/SplitText'
import {
forwardRef,
useEffect,
useImperativeHandle,
useMemo,
useRef,
useState,
} from 'react'
import s from './split-text.module.scss'

gsap.registerPlugin(GSAPSplitText)

export const SplitText = forwardRef(function SplitText(
{ children, className, type },
ref,
) {
const elementRef = useRef()
const [setRectRef, rect] = useRect()

const [splitted, setSplitted] = useState()

useImperativeHandle(ref, () => splitted, [splitted])

useEffect(() => {
const splitted = new GSAPSplitText(elementRef.current, {
tag: 'span',
type,
})
setSplitted(splitted)

return () => {
splitted.revert()
setSplitted()
}
}, [rect, type])

const render = useMemo(
() => (
<span className={className}>
<span className={s.wrapper}>
<span
style={{ visibility: 'hidden', display: 'inline-block' }}
ref={setRectRef}
>
{children}
</span>
<span ref={elementRef} className={s.splitText}>
{children}
</span>
</span>
</span>
),
[children, className],
)

return render
})
9 changes: 9 additions & 0 deletions components/split-text/split-text.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.wrapper {
display: inline-block;
position: relative;

.splitText {
position: absolute;
inset: 0;
}
}
26 changes: 0 additions & 26 deletions hooks/use-ready-state.js

This file was deleted.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@
"overrides": {
"react": "18.2.0",
"react-dom": "18.2.0",
"scheduler": "0.23.0"
"scheduler": "0.23.0",
"gsap": "npm:@gsap/business@^3.12.2",
"@studio-freight/hamo": "0.6.21"
}
},
"lint-staged": {
Expand Down
8 changes: 4 additions & 4 deletions pages/home/home.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
align-items: center;
justify-content: center;
min-height: 100vh;

.title {
flex-grow: 1;
}
text-align: justify;
font-size: desktop-vw(16px);
max-width: desktop-vw(500px);
margin: auto;
}
}
52 changes: 51 additions & 1 deletion pages/home/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,61 @@
import { SplitText } from 'components/split-text'
import { colors } from 'config/variables'
import gsap from 'gsap'
import { Layout } from 'layouts/default'
import { useEffect, useState } from 'react'
import s from './home.module.scss'

export default function Home() {
const [titleSplitted, setTitleSplitted] = useState()

useEffect(() => {
const words = titleSplitted?.words

if (!words) return

words.forEach((word) => {
word.addEventListener(
'mouseenter',
() => {
gsap.fromTo(
word,
{
color: colors.green,
},
{
color: colors.white,
duration: 4,
ease: 'expo.out',
},
)
},
false,
)
})
}, [titleSplitted])

return (
<Layout theme="dark" className={s.home}>
<section className={s.content}>
<h1 className={s.title}>satus</h1>
<h1 className={s.title}>
<SplitText
ref={(node) => {
if (node) setTitleSplitted(node)
}}
type="words"
>
Inceptum pulchrum, iterum incipiamus amici, Res novas aggrediamur,
virtute superbi. Aurora nova luce, spes nobis enitescat, Sicut sol
oriens, ex tenebris emergat. Ingenium nostrum flamma lucida flagret,
Consilium firmum, studium nos agitet. Labor et laboris fructus nobis
faveant, Perseverantia vincat, ita crescamus ameant. Principium est
fundamentum, quod nobis est datum, Inceptum pulchrum, munus magnum,
probatum. Manus ad aratrum, cor ad caelum dirigamus, Opus magnum
faciamus, vitam renovemus. Spes in pectore, etiam si nox obscurat,
Perseverando vincemus, quodcunque laborat. Inceptum pulchrum,
studium nostrum lucidum, Fama et gloria nobis, sic sit perpetuum.
</SplitText>
</h1>
</section>
</Layout>
)
Expand Down
51 changes: 31 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 090b555

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"⚡️ Lighthouse report for the changes in this commit:

🟠 Performance: 56
🟢 Accessibility: 97
🟢 Best practices: 100
🟠 SEO: 75
🔴 PWA: 30

Lighthouse ran on https://astro-satus-d7tuymmgx-studio-freight.vercel.app/"

Please sign in to comment.