Skip to content

Commit

Permalink
✨ Add rocket 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
homostellaris committed Nov 14, 2024
1 parent 33ba36d commit 7182225
Show file tree
Hide file tree
Showing 17 changed files with 586 additions and 514 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion components/common/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const Header = ({ title }: { title: string }) => {
<IonImg
src="/icon.png"
slot="start"
className="w-10 h-10 ml-4"
className="w-10 h-10 ml-2" // Needs to align with starship and trajectory
/>
<IonTitle>{title}</IonTitle>
<IonButtons
Expand Down
21 changes: 21 additions & 0 deletions components/common/Starship.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Image from 'next/image'
import { cn } from './cn'

import React, { forwardRef } from 'react'

const Starship = forwardRef<HTMLImageElement, { className?: string }>(
function Starship({ className }, ref) {
return (
<Image
alt="A starship from a birds-eye view"
className={cn('starship', className)}
src="/starship.png"
layout="fill"
quality={100}
ref={ref}
></Image>
)
},
)

export default Starship
6 changes: 6 additions & 0 deletions components/common/cn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { clsx, type ClassValue } from 'clsx'
import { twMerge } from 'tailwind-merge'

export function cn(...args: ClassValue[]) {
return twMerge(clsx(args))
}
47 changes: 36 additions & 11 deletions components/demo/Journey/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createRef, useLayoutEffect, useRef, useState } from 'react'
import Starship from '../../landingPage/Journey/Starship'
import { createRef, useEffect, useLayoutEffect, useRef, useState } from 'react'
import Starship from '../../common/Starship'
import Todos from '../../todos'
import { Todo } from '../../todos/interfaces'
import { useWindowSize } from '../../common/useWindowResize'
Expand All @@ -9,14 +9,14 @@ export default function Journey({ todos }: { todos: Todo[] }) {
const todosRef = createRef<HTMLDivElement>()
useMaintainScrollOffset(currentTodoRef, todosRef)

const [starshipY, _] = useStarshipYPosition(currentTodoRef)
// const [starshipY, _] = useStarshipYPosition(currentTodoRef)

return (
<div className="relative w-4/6 p-20 m-auto">
<div
id="starship"
className={`absolute left-0 m-auto h-[10vmin] w-[10vmin] transition-transform`}
style={{ transform: `translateY(${starshipY}px)` }}
style={{ transform: `translateY(${0}px)` }}
>
<Starship />
</div>
Expand Down Expand Up @@ -50,18 +50,43 @@ function useMaintainScrollOffset(currentTodoRef, todosRef) {
}, [currentTodoRef, todosRef])
}

function useStarshipYPosition(currentTodoRef) {
export function useStarshipYPosition(
starship: HTMLElement | null,
nextTodoPosition: DOMRect | null,
commonAncestor: HTMLElement | null,
) {
console.log('Starship position render')
const size = useWindowSize()
const [starshipY, setStarshipY] = useState<number>(0)

// TODO: This causes it to render a second time when a todo completion changes the scroll height. But maybe that doesn't matter and maybe we can move the starship into its own component hierarchy so it doesn't re-render other things unnecessarily.
useLayoutEffect(() => {
const elementPadding = 80
const starshipImagePadding = 10
setStarshipY(
currentTodoRef.current.offsetTop - elementPadding - starshipImagePadding,
useEffect(() => {
console.log('Starship position effect')
if (
starship === null ||
nextTodoPosition === null ||
nextTodoPosition.height === null ||
commonAncestor === null
)
}, [currentTodoRef, size, setStarshipY])
return setStarshipY(0)

const commonAncestorRect = commonAncestor.getBoundingClientRect()
const todoDistanceFromCommonAncestor =
nextTodoPosition.y - commonAncestorRect.y
const starshipHeightAdjustment =
(nextTodoPosition.height - starship?.offsetHeight) / 2

const y = todoDistanceFromCommonAncestor + starshipHeightAdjustment
console.log(`Setting startship Y to ${y}`, {
// todoId: nextTodo.dataset.id,
// hasCorrectAttr: nextTodo.dataset.nextTodo,
commonAncestorRect,
nextTodoPosition,
todoDistanceFromCommonAncestor,
starshipHeightAdjustment,
})
setStarshipY(y)
}, [commonAncestor, nextTodoPosition, size, starship, setStarshipY])

return [starshipY, setStarshipY]
}
13 changes: 0 additions & 13 deletions components/landingPage/Journey/Starship.tsx

This file was deleted.

15 changes: 13 additions & 2 deletions components/landingPage/Journey/Trajectory.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
export default function Tracjectory() {
return <div className="trajectory h-full w-[2px] bg-supernova"></div>
import { cn } from '../../common/cn'

export default function Tracjectory(props: JSX.IntrinsicElements['div']) {
return (
<div
className={cn('trajectory h-full w-[2px] bg-supernova', props.className)}
// Make the mask solid behind the rocket for a 'completed' effect
style={{
maskImage: 'repeating-linear-gradient(180deg, black, transparent 10px)',
// maskSize: '10px 100%',
}}
></div>
)
}
2 changes: 1 addition & 1 deletion components/landingPage/Journey/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Starship from './Starship'
import Starship from '../../common/Starship'
import Tracjectory from './Trajectory'

export default function Journey() {
Expand Down
2 changes: 1 addition & 1 deletion components/landingPage/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Footer from '../common/Footer'
import Planets from '../common/Planets'
import { ScrollDirection } from '../common/scroll'
import Contact from './Contact'
import Starship from './Journey/Starship'
import Starship from '../common/Starship'
import Tracjectory from './Journey/Trajectory'

const Layout = ({
Expand Down
Loading

0 comments on commit 7182225

Please sign in to comment.