Skip to content

Commit 1c3d70a

Browse files
committed
(chore!): animation classes defined
1 parent 1f95432 commit 1c3d70a

File tree

15 files changed

+257
-58
lines changed

15 files changed

+257
-58
lines changed

src/components/animatedModal/AnimatedModal.enums.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/components/animatedModal/AnimatedModal.stories.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ import React, { useRef } from 'react';
55
import {
66
AnimatedModal,
77
AnimatedModalObject,
8-
ModalAnimation,
9-
AnimatedModalFrame
8+
AnimatedModalFrame,
9+
Unfold,
10+
Reveal,
11+
Uncover,
12+
Flash,
13+
Sketch,
14+
Slide,
15+
BlowUp
1016
} from '../../components';
1117

1218
export default {
@@ -20,37 +26,37 @@ const AnimatedModalTemplate: Story = () => {
2026

2127
return (
2228
<>
23-
<AnimatedModal ref={ref} animation={ModalAnimation.Unfold} />
29+
<AnimatedModal ref={ref} animation={new Unfold()} />
2430
<AnimatedModalFrame>
2531
<button onClick={() => ref.current?.OpenModal()}>Open Unfold Modal</button>
2632
<br />
2733
<br />
28-
<button onClick={() => ref.current?.OpenModal(ModalAnimation.Reveal)}>
34+
<button onClick={() => ref.current?.OpenModal(Reveal.animationName)}>
2935
Open Reveal Modal
3036
</button>
3137
<br />
3238
<br />
33-
<button onClick={() => ref.current?.OpenModal(ModalAnimation.Uncover)}>
39+
<button onClick={() => ref.current?.OpenModal(Uncover.animationName)}>
3440
Open Uncover Modal
3541
</button>
3642
<br />
3743
<br />
38-
<button onClick={() => ref.current?.OpenModal(ModalAnimation.Flash)}>
44+
<button onClick={() => ref.current?.OpenModal(Flash.animationName)}>
3945
Open Flash Modal
4046
</button>
4147
<br />
4248
<br />
43-
<button onClick={() => ref.current?.OpenModal(ModalAnimation.Sketch)}>
49+
<button onClick={() => ref.current?.OpenModal(Sketch.animationName)}>
4450
Open Sketch Modal
4551
</button>
4652
<br />
4753
<br />
48-
<button onClick={() => ref.current?.OpenModal(ModalAnimation.Slide)}>
54+
<button onClick={() => ref.current?.OpenModal(Slide.animationName)}>
4955
Open Slide Modal
5056
</button>
5157
<br />
5258
<br />
53-
<button onClick={() => ref.current?.OpenModal(ModalAnimation.BlowUp)}>
59+
<button onClick={() => ref.current?.OpenModal(BlowUp.animationName)}>
5460
Open BlowUp Modal
5561
</button>
5662
<br />

src/components/animatedModal/AnimatedModal.tsx

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useEffect, useState, useImperativeHandle, forwardRef } from 'rea
22

33
import './AnimatedModal.styles.scss';
44

5-
import { ModalAnimation } from './AnimatedModal.enums';
5+
import { ModalAnimation, Sketch, Unfold } from './animations';
66

77
/**
88
* Animated Modal's props.
@@ -13,34 +13,70 @@ import { ModalAnimation } from './AnimatedModal.enums';
1313
* @property {boolean} [closeOnBackgroundClick] Close modal on background click?
1414
*/
1515
interface IAnimatedModalProps {
16-
isOpen?: boolean;
16+
/**
17+
* Open Modal on startup.
18+
* @param {boolean} [startOpen] Open Modal on startup?
19+
* @default false
20+
*/
21+
startOpen?: boolean;
22+
23+
/**
24+
* Modal animation.
25+
* @param {ModalAnimation} animation Modal animation
26+
* @default new Unfold()
27+
*/
1728
animation: ModalAnimation;
29+
30+
/**
31+
* Modal's children.
32+
* @param {React.ReactNode} [children] Modal's children
33+
*/
1834
children?: React.ReactNode;
35+
36+
/**
37+
* Close modal on background click.
38+
* @param {boolean} [closeOnBackgroundClick] Close modal on background click?
39+
* @default true
40+
*/
1941
closeOnBackgroundClick?: boolean;
2042
}
2143

2244
/**
2345
* Animated Modal's object.
2446
* @type AnimatedModalObject
25-
* @property {() => void} OpenModal Open modal
26-
* @property {() => void} CloseModal Close modal
27-
* @property {() => boolean} IsModalOpen Is modal open?
47+
* @member {() => void} OpenModal Open modal
48+
* @member {() => void} CloseModal Close modal
49+
* @member {() => boolean} IsModalOpen Is modal open?
2850
*/
2951
export type AnimatedModalObject = {
52+
/**
53+
* Open modal.
54+
* @param {string} [animation] Modal animation
55+
* @returns {void} void
56+
*/
3057
// eslint-disable-next-line no-unused-vars
31-
OpenModal: (modalAnimation?: ModalAnimation) => void;
58+
OpenModal: (animation?: string) => void;
59+
60+
/**
61+
* Close modal with open modal animation.
62+
* @returns {void} void
63+
*/
3264
CloseModal: () => void;
65+
66+
/**
67+
* Check if modal is open.
68+
* @returns {boolean} is modal open?
69+
*/
3370
IsModalOpen: () => boolean;
3471
};
3572

3673
/**
3774
* Animated Modal.
38-
*
3975
* @param {IAnimatedModalProps} props Animated modal's props
4076
* @param {React.Ref<AnimatedModalObject>} ref Animated modal's ref
4177
* @returns AnimatedModal.
4278
*/
43-
const AnimatedModal = forwardRef(
79+
export const AnimatedModal = forwardRef(
4480
(props: IAnimatedModalProps, ref: React.Ref<AnimatedModalObject>) => {
4581
// State
4682
const [modalClass, setModalClass] = useState('');
@@ -60,12 +96,12 @@ const AnimatedModal = forwardRef(
6096

6197
/**
6298
* Open modal.
63-
* @param {ModalAnimation} modalAnimation Modal animation
99+
* @param {string} animation Modal animation
64100
* @returns {void}
65101
*/
66-
function openModal(modalAnimation?: ModalAnimation): void {
67-
if (modalAnimation) setModalClass(modalAnimation);
68-
else setModalClass(props.animation);
102+
function openModal(animation?: string): void {
103+
if (animation) setModalClass(animation);
104+
else setModalClass(props.animation.getAnimationName());
69105

70106
document.body.classList.add('modal-active');
71107
}
@@ -115,11 +151,11 @@ const AnimatedModal = forwardRef(
115151
}
116152

117153
useEffect(() => {
118-
if (props.isOpen) {
119-
setModalClass(props.animation);
154+
if (props.startOpen) {
155+
setModalClass(props.animation.getAnimationName());
120156
document.body.classList.add('modal-active');
121157
}
122-
}, [props.isOpen]);
158+
}, [props.startOpen]);
123159

124160
/**
125161
* Default modal.
@@ -139,20 +175,28 @@ const AnimatedModal = forwardRef(
139175
);
140176
}
141177

178+
function sketchSVG(): React.ReactElement {
179+
return (
180+
<svg
181+
id="modal-sketch-svg"
182+
xmlns="http://www.w3.org/2000/svg"
183+
width="100%"
184+
height="100%"
185+
preserveAspectRatio="none">
186+
<rect x="0" y="0" fill="none" width="226" height="162" rx="3" ry="3"></rect>
187+
</svg>
188+
);
189+
}
190+
191+
console.log(modalClass);
192+
142193
return (
143194
<div id="animated-modal-container" className={modalClass}>
144195
<div
145196
id="animated-modal-background"
146197
onClick={props.closeOnBackgroundClick ? onBackgroundClick : undefined}>
147198
<div id="animated-modal">
148-
<svg
149-
id="modal-sketch-svg"
150-
xmlns="http://www.w3.org/2000/svg"
151-
width="100%"
152-
height="100%"
153-
preserveAspectRatio="none">
154-
<rect x="0" y="0" fill="none" width="226" height="162" rx="3" ry="3"></rect>
155-
</svg>
199+
{modalClass.includes(Sketch.animationName) ? sketchSVG() : undefined}
156200
<div id="modal-content">{props.children ? props.children : defaultModal()}</div>
157201
</div>
158202
</div>
@@ -166,9 +210,7 @@ AnimatedModal.displayName = 'AnimatedModal';
166210

167211
// Set default props
168212
AnimatedModal.defaultProps = {
169-
isOpen: false,
170-
animation: ModalAnimation.Unfold,
213+
startOpen: false,
214+
animation: new Unfold(),
171215
closeOnBackgroundClick: true
172216
};
173-
174-
export default AnimatedModal;

src/components/animatedModal/AnimatedModalFrame.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ interface IAnimatedModalProps {
44
children?: React.ReactNode;
55
}
66

7-
const AnimatedModalFrame: FC<IAnimatedModalProps> = (props) => {
7+
export const AnimatedModalFrame: FC<IAnimatedModalProps> = (props) => {
88
return <div id="animated-modal-frame">{props.children}</div>;
99
};
10-
11-
export default AnimatedModalFrame;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { ModalAnimation } from './ModalAnimation';
2+
3+
/**
4+
* BlowUp animation
5+
* @class BlowUp
6+
* @constructor
7+
* @param {
8+
* animationName: string
9+
* }
10+
* @extends ModalAnimation
11+
* @example
12+
* new BlowUp();
13+
*/
14+
export class BlowUp extends ModalAnimation {
15+
static animationName = 'blowUp';
16+
17+
constructor() {
18+
super(BlowUp.animationName);
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { ModalAnimation } from './ModalAnimation';
2+
3+
/**
4+
* Flash animation
5+
* @class Flash
6+
* @constructor
7+
* @param {
8+
* animationName: string
9+
* }
10+
* @extends ModalAnimation
11+
* @example
12+
* new Flash();
13+
*/
14+
export class Flash extends ModalAnimation {
15+
static animationName = 'flash';
16+
17+
constructor() {
18+
super(Flash.animationName);
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* ModalAnimation
3+
* @abstract
4+
* @class ModalAnimation
5+
* @constructor with animationName
6+
* @method getAnimationName
7+
*/
8+
export abstract class ModalAnimation {
9+
// Name of the animation
10+
private animationName: string;
11+
12+
// Constructor
13+
constructor(animationName: string) {
14+
this.animationName = animationName;
15+
}
16+
17+
getAnimationName(): string {
18+
return this.animationName;
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { ModalAnimation } from './ModalAnimation';
2+
3+
/**
4+
* Reveal animation
5+
* @class Reveal
6+
* @constructor
7+
* @param {
8+
* animationName: string
9+
* }
10+
* @extends ModalAnimation
11+
* @example
12+
* new Reveal();
13+
*/
14+
export class Reveal extends ModalAnimation {
15+
static animationName = 'reveal';
16+
17+
constructor() {
18+
super(Reveal.animationName);
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { ModalAnimation } from './ModalAnimation';
2+
3+
/**
4+
* Sketch animation
5+
* @class Sketch
6+
* @constructor
7+
* @param {
8+
* animationName: string
9+
* }
10+
* @extends ModalAnimation
11+
* @example
12+
* new Sketch();
13+
*/
14+
export class Sketch extends ModalAnimation {
15+
static animationName = 'sketch';
16+
17+
constructor() {
18+
super(Sketch.animationName);
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { ModalAnimation } from './ModalAnimation';
2+
3+
/**
4+
* Slide animation
5+
* @class Slide
6+
* @constructor
7+
* @param {
8+
* animationName: string
9+
* }
10+
* @extends ModalAnimation
11+
* @example
12+
* new Slide();
13+
*/
14+
export class Slide extends ModalAnimation {
15+
static animationName = 'slide';
16+
17+
constructor() {
18+
super(Slide.animationName);
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { ModalAnimation } from './ModalAnimation';
2+
3+
/**
4+
* Uncover animation
5+
* @class Uncover
6+
* @constructor
7+
* @param {
8+
* animationName: string
9+
* }
10+
* @extends ModalAnimation
11+
* @example
12+
* new Uncover();
13+
*/
14+
export class Uncover extends ModalAnimation {
15+
static animationName = 'uncover';
16+
17+
constructor() {
18+
super(Uncover.animationName);
19+
}
20+
}

0 commit comments

Comments
 (0)