Skip to content

Commit

Permalink
docs: add react hook demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernankez committed Apr 1, 2024
1 parent 55327ab commit 4bca6b4
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 3 deletions.
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export default bernankez({
unocss: true,
astro: true,
formatters: true,
vue: true,
});
1 change: 1 addition & 0 deletions playground/public/react.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions playground/public/vue.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions playground/src/demos/WithReact.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { useBackmoji, useImageLoader, useImageRenderer } from "@backmoji/react";
import ReactLogo from "/react.svg?url";
import { useEffect, useRef } from "react";
import { useLatest } from "ahooks";
import { useResizeObserver } from "../hooks/useResizeObserver";

export function WithReact() {
const canvasRef = useRef<HTMLCanvasElement>(null);

const img = useImageLoader(ReactLogo);
const renderer = useImageRenderer(img);
const backmojiResult = useBackmoji(renderer, canvasRef, {
height: 150,
rowGap: 15,
columnGap: 30,
});
const { render, canvas, setSize } = useLatest(backmojiResult).current;
const divRef = useResizeObserver<HTMLDivElement>((entries) => {
for (const entry of entries) {
const { width, height } = entry.contentRect;
setSize?.(width, height);
render?.();
}
});

useEffect(() => {
if (canvas) {
setSize?.(divRef.current?.clientWidth, divRef.current?.clientHeight);
render?.();

return () => {
canvas.remove();
};
}
}, [canvas]);

return (
<div ref={divRef}>
<canvas ref={canvasRef}></canvas>
</div>
);
}
33 changes: 33 additions & 0 deletions playground/src/hooks/useResizeObserver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useLatest } from "ahooks";
import { useEffect, useRef, useState } from "react";

export function useResizeObserver<T extends HTMLElement>(
callback: ResizeObserverCallback,
options?: ResizeObserverOptions,
): React.RefObject<T> {
const ref = useRef<T>(null);
const callbackRef = useLatest(callback);

useEffect(() => {
const { current } = ref;

if (!current) {
return;
}

const cb: ResizeObserverCallback = (entries, observer) => {
return callbackRef.current(entries, observer);
};

const observer = new ResizeObserver(cb);
observer.observe(current, options);

return () => {
if (current) {
observer.unobserve(current);
}
};
}, [options]);

return ref;
}
10 changes: 7 additions & 3 deletions playground/src/pages/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,12 @@ render();

### Render with animation

### Use with React
### Using with React

### Use with Vue
import { WithReact } from "@/demos/WithReact";

### Options
<WithReact client:visible />

### Using with Vue

### Options

0 comments on commit 4bca6b4

Please sign in to comment.