Skip to content

Commit

Permalink
docs: add example with r3f
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelass committed Dec 23, 2022
1 parent 78de45b commit 68b84a7
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@
"import/no-unassigned-import": "off"
}
},
{
"files": "stories/r3f.tsx",
"rules": {
"react/no-unknown-property": "off"
}
},
{
"files": "*.config.mjs",
"rules": {
Expand Down
2 changes: 1 addition & 1 deletion .storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const process = require("node:process");
const toPath = path_ => path.join(process.cwd(), path_);

module.exports = {
stories: ["../src/**/*.stories.mdx", "../src/**/*.stories.@(js|jsx|ts|tsx)"],
stories: ["../stories/**/*.stories.mdx", "../stories/**/*.stories.@(js|jsx|ts|tsx)"],
addons: ["@storybook/addon-links", "@storybook/addon-essentials", "@storybook/addon-docs"],
framework: "@storybook/react",
typescript: { reactDocgen: true },
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@jest/globals": "^29.2.2",
"@mdx-js/react": "^1.6.22",
"@react-three/fiber": "^8.9.1",
"@storybook/addon-actions": "^6.5.14",
"@storybook/addon-essentials": "^6.5.14",
"@storybook/addon-interactions": "^6.5.14",
Expand Down Expand Up @@ -138,6 +139,7 @@
"jest": "^29.2.2",
"jest-config": "^29.2.2",
"jest-environment-jsdom": "^29.2.2",
"jotai": "^1.12.0",
"lint-staged": "^13.0.3",
"next": "^13.1.0",
"prettier": "^2.7.1",
Expand All @@ -146,6 +148,7 @@
"rollup": "^3.2.5",
"rollup-plugin-swc3": "^0.7.0",
"swc-plugin-coverage-instrument": "^0.0.14",
"three": "^0.148.0",
"tslib": "^2.4.1",
"type-fest": "^3.1.0",
"typescript": "^4.8.4"
Expand Down
53 changes: 50 additions & 3 deletions src/examples.stories.tsx → stories/examples.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { css, Global } from "@emotion/react";
import styled from "@emotion/styled";
import { Canvas } from "@react-three/fiber";
import { useEffect, useState } from "@storybook/addons";
import type { ComponentStory, ComponentMeta } from "@storybook/react";
import type { ComponentMeta, ComponentStory } from "@storybook/react";
import type { DetailedHTMLProps, HTMLAttributes } from "react";
import { forwardRef, useRef } from "react";

import { CLASS_NAMES, CSS_VARS } from "./constants";
import StickyRoll from "./stickyroll";
import type { UseStickyrollOptions } from "../src";
import { CLASS_NAMES, CSS_VARS } from "../src";
import StickyRoll from "../src/stickyroll";
import useStickyroll from "../src/use-stickyroll";

import { progressRef, Scene } from "./r3f";

const Flex = styled("div")({
position: "relative",
Expand Down Expand Up @@ -422,3 +427,45 @@ export const Apple = AppleBase.bind({});
Apple.args = {
factor: 3,
};

const R3fWrapper = styled("div")({
minHeight: "var(--100vh, 100vh)",
height: `var(${CSS_VARS.height}, var(--100vh, 100vh))`,
});

const R3fInner = styled("div")({
position: "sticky",
inset: 0,
height: `var(--100vh, 100vh)`,
});

function R3FBase(args: UseStickyrollOptions) {
const ref = useRef<HTMLDivElement>(null);
useStickyroll(ref, {
...args,
onProgress(progress) {
progressRef.current = progress;
},
pages: 1,
});

return (
<>
<Global styles={css({ ".sb-show-main.sb-main-padded": { padding: 0 } })} />
<div style={{ height: "100vh" }} />
<R3fWrapper ref={ref}>
<R3fInner>
<Canvas orthographic camera={{ far: 1000, near: -1000 }}>
<Scene />
</Canvas>
</R3fInner>
</R3fWrapper>
<div style={{ height: "100vh" }} />
</>
);
}
export const R3F = R3FBase.bind({});

R3F.args = {
factor: 3,
};
51 changes: 51 additions & 0 deletions stories/r3f.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { useFrame } from "@react-three/fiber";
import { useRef } from "react";
import * as THREE from "three";
import type { Group } from "three";

THREE.ColorManagement.legacyMode = false;

export const progressRef = { current: 0 };
export const geometry = new THREE.ShapeGeometry(
new THREE.Shape()
.moveTo(-0.5, -1.5)
.lineTo(0.5, -1.5)
.lineTo(0.5, 0.5)
.lineTo(0.5, 1.5)
.lineTo(-0.5, 0.5)
.lineTo(-0.5, -1.5)
);
export const material = new THREE.MeshStandardMaterial({
color: "#673AB7",
side: THREE.DoubleSide,
wireframe: false,
});

export function Scene() {
const ref = useRef<Group>(null);

useFrame(() => {
ref.current.rotation.x = Math.PI * 2 * progressRef.current + Math.PI * 0.804;
ref.current.rotation.y = Math.PI * 2 * progressRef.current + Math.PI * 0.75;
});

return (
<>
<color attach="background" args={["#feda21"]} />
<ambientLight />
<directionalLight position={[-100, 300, 400]} />
<group ref={ref} scale={100} position={[0, -100, 0]}>
<mesh position={[2, 0, 0.5]} material={material} geometry={geometry}></mesh>
<mesh position={[2.5, 0, 0]} rotation={[0, Math.PI / 2, 0]} material={material}>
<planeGeometry args={[1, 5]} />
</mesh>
<mesh position={[0, -2, 0]} material={material}>
<boxGeometry args={[5, 1, 1]} />
</mesh>
<mesh position={[-2, -2, -2]} material={material}>
<boxGeometry args={[1, 1, 5]} />
</mesh>
</group>
</>
);
}
4 changes: 2 additions & 2 deletions src/stickyroll.stories.tsx → stories/stickyroll.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ComponentStory, ComponentMeta } from "@storybook/react";
import { useRef } from "react";

import { CSS_VARS } from "./constants";
import Stickyroll from "./stickyroll";
import { CSS_VARS } from "../src";
import Stickyroll from "../src/stickyroll";

export default {
title: "Stickyroll",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { ComponentStory, ComponentMeta } from "@storybook/react";
import type { ReactNode } from "react";
import { useRef } from "react";

import { CLASS_NAMES, CSS_VARS, STYLE } from "./constants";
import type { UseStickyrollOptions } from "./types";
import useStickyroll from "./use-stickyroll";
import { CLASS_NAMES, CSS_VARS, STYLE } from "../src";
import type { UseStickyrollOptions } from "../src";
import useStickyroll from "../src/use-stickyroll";

function Stickyroll({ children, ...args }: UseStickyrollOptions & { children?: ReactNode }) {
const ref = useRef<HTMLDivElement>(null);
Expand Down
Loading

1 comment on commit 68b84a7

@vercel
Copy link

@vercel vercel bot commented on 68b84a7 Dec 23, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

react-stickyroll – ./

react-stickyroll.vercel.app
react-stickyroll-pixelass.vercel.app
react-stickyroll-git-main-pixelass.vercel.app

Please sign in to comment.