This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
generated from storybookjs/addon-kit
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from storybookjs/valentin/confetti-wrapper
Implement bare minimum confetti component
- Loading branch information
Showing
4 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Meta, StoryObj } from "@storybook/react"; | ||
import { Confetti } from "./Confetti"; | ||
import React from "react"; | ||
|
||
const meta: Meta<typeof Confetti> = { | ||
component: Confetti, | ||
parameters: { | ||
chromatic: { disableSnapshot: true }, | ||
}, | ||
decorators: [ | ||
(StoryFn) => ( | ||
<div style={{ height: "100vh", width: "100vw" }}> | ||
<button>I am clickable</button> | ||
<StoryFn /> | ||
</div> | ||
), | ||
], | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof Confetti>; | ||
|
||
export const FullWidth: Story = {}; | ||
|
||
export const Positioned: Story = { | ||
args: { | ||
top: 100, | ||
left: 300, | ||
width: 300, | ||
height: 250, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import ReactConfetti from "react-confetti"; | ||
import React, { useEffect, useRef } from "react"; | ||
import { styled } from "@storybook/theming"; | ||
import { createPortal } from "react-dom"; | ||
import { useState } from "react"; | ||
|
||
interface ConfettiProps | ||
extends Omit<React.ComponentProps<typeof ReactConfetti>, "drawShape"> { | ||
top: number; | ||
left: number; | ||
width: number; | ||
height: number; | ||
} | ||
|
||
const Wrapper = styled.div<{ | ||
width: number; | ||
height: number; | ||
top: number; | ||
left: number; | ||
}>(({ width, height, left, top }) => ({ | ||
width: `${width}px`, | ||
height: `${height}px`, | ||
left: `${left}px`, | ||
top: `${top}px`, | ||
position: "relative", | ||
overflow: "hidden", | ||
})); | ||
|
||
export function Confetti({ | ||
top = 0, | ||
left = 0, | ||
width = window.innerWidth, | ||
height = window.innerHeight, | ||
...confettiProps | ||
}: ConfettiProps) { | ||
const [confettiContainer] = useState(() => { | ||
const container = document.createElement("div"); | ||
container.setAttribute("id", "confetti-container"); | ||
container.setAttribute( | ||
"style", | ||
"position: fixed; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none; z-index: 9999;" | ||
); | ||
|
||
return container; | ||
}); | ||
|
||
useEffect(() => { | ||
document.body.appendChild(confettiContainer); | ||
|
||
return () => { | ||
document.body.removeChild(confettiContainer); | ||
}; | ||
}, []); | ||
|
||
return createPortal( | ||
<Wrapper top={top} left={left} width={width} height={height}> | ||
<ReactConfetti {...confettiProps} /> | ||
</Wrapper>, | ||
confettiContainer | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters