Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: Add confetti component #1188

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"lodash.without": "^4.4.0",
"memoize-one": "^5.1.1",
"prop-types": "^15.7.2",
"react-confetti": "^6.1.0",
"react-imask": "^6.2.2",
"react-resize-detector": "^4.2.3",
"react-select-plus": "1.2.0",
Expand Down
110 changes: 110 additions & 0 deletions src/components/Confetti/Confetti.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { array, boolean, number } from '@storybook/addon-knobs';
import React, { useState } from 'react';
import Button from '../Button/Button';
import ButtonToolbar from '../Button/ButtonToolbar';

import Modal from '../Modal/Modal';
import ModalBody from '../Modal/ModalBody';
import ModalFooter from '../Modal/ModalFooter';
import ModalHeader from '../Modal/ModalHeader';
import ConfettiDropper from './Confetti';

export default {
title: 'Confetti',
component: ConfettiDropper,
parameters: {
sourceLink: 'Modal/Modal.js',
},
};

export const SimpleExample = () => {
const [open, setOpen] = useState(false);
return (
<>
<Button onClick={() => setOpen(true)}>Open Simple Modal</Button>
<Modal isOpen={open} fade={false} fullscreen={null} size={null}>
<ConfettiDropper
numberOfPieces={number('numberOfPieces', 1000)}
fullWidth={boolean('fullWidth', false)}
recycle={false}
/>
<ModalHeader toggle={() => setOpen(false)}>Modal title</ModalHeader>
<ModalBody>Congrats!!!!</ModalBody>
<ModalFooter>
<ButtonToolbar>
<Button color="primary" onClick={() => setOpen(false)}>
Thank you
</Button>
<Button onClick={() => setOpen(false)}>Cancel</Button>
</ButtonToolbar>
</ModalFooter>
</Modal>
</>
);
};

export const HeartsExample = () => {
const [open, setOpen] = useState(false);
return (
<>
<Button onClick={() => setOpen(true)}>♥ Open Hearts Modal ♥</Button>
<Modal isOpen={open} fade={false} fullscreen={null} size={null}>
<ConfettiDropper
numberOfPieces={number('numberOfPieces', 1000)}
fullWidth={boolean('fullWidth', false)}
colors={[
'#f8c9eb',
'#f8e4e2',
'#fe9dd0',
'#eb78B8',
'#8bd2f1',
'#FF269B',
'#FEA1E7',
'#FBB657',
'#FE4D84',
'#6187DB',
]}
drawShape={(ctx) => {
ctx.save();
ctx.beginPath();
const dim = 20;
const offset = dim / 5;
ctx.moveTo(offset + 0, offset + dim / 2);
ctx.lineTo(offset + dim / 2, offset + dim);
ctx.lineTo(offset + dim, offset + dim / 2);
ctx.moveTo(offset, offset + dim / 2);
const radius = Math.sqrt((dim * dim) / 4 + (dim * dim) / 4) / 2;
ctx.arc(
offset + dim / 4,
offset + dim / 4,
radius,
(Math.PI * 3) / 4,
(Math.PI * 7) / 4
);
ctx.arc(
offset + (3 * dim) / 4,
offset + dim / 4,
radius,
(Math.PI * 5) / 4,
Math.PI / 4
);
ctx.closePath();
ctx.fill();
ctx.restore();
}}
recycle={false}
/>
<ModalHeader toggle={() => setOpen(false)}>Modal title</ModalHeader>
<ModalBody>♥♥♥♥♥♥♥♥ Congrats!!!! ♥♥♥♥♥♥♥♥</ModalBody>
<ModalFooter>
<ButtonToolbar>
<Button color="primary" onClick={() => setOpen(false)}>
Thank you
</Button>
<Button onClick={() => setOpen(false)}>Cancel</Button>
</ButtonToolbar>
</ModalFooter>
</Modal>
</>
);
};
31 changes: 31 additions & 0 deletions src/components/Confetti/Confetti.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React, { FC, useCallback, useState } from 'react';
import Confetti from 'react-confetti';

export interface ConfettiProps {
fullWidth?: boolean;
}

const ConfettiDropper: FC<ConfettiProps> = ({
fullWidth = true,
...props
}) => {
props = {
...(fullWidth && { style: { position: 'fixed' }, height: window.innerHeight }),
...props
}
const [width, setWidth] = useState(0);

const div = useCallback((node) => {
if (node !== null) {
// set width to width of div or screen
setWidth(fullWidth ? window.innerWidth : node.getBoundingClientRect().width);
}
}, [fullWidth]);
return (
<div ref={div}>
<Confetti {...props} width={width} />
</div>
);
};

export default ConfettiDropper;
Loading