Skip to content

Commit

Permalink
jsx style component
Browse files Browse the repository at this point in the history
  • Loading branch information
sujon-co committed Jan 22, 2022
1 parent 365de7f commit 8207a9d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.7.6",
"version": "1.7.8",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
16 changes: 12 additions & 4 deletions src/components/Draggable.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import React, { FC, useCallback, useEffect, useRef, useState } from 'react';
import styles from '../style/style.module.css';
import * as styles from '../style/styles';

export const Draggable: FC = ({ children }) => {
interface IProps {
bodyStyle: React.CSSProperties;
}

export const Draggable: FC<IProps> = ({ children, bodyStyle }) => {
const boxRef = useRef<HTMLDivElement>(null);
const dragRef = useRef<HTMLDivElement>(null);
const [isDragActive, setIsDragActive] = useState<boolean>(false);
Expand Down Expand Up @@ -47,8 +51,12 @@ export const Draggable: FC = ({ children }) => {
}, [dragEndHandler, dragStartHandler, draggingHandler]);

return (
<div ref={boxRef} className={styles.ReactPortalDialog__Body}>
<div ref={dragRef} className={styles.ReactPortalDialog__Move}>
<div ref={boxRef} style={bodyStyle} className="ReactPortalDialog__Body">
<div
ref={dragRef}
style={styles.ReactPortalDialog__Move}
className="ReactPortalDialog__Move"
>
Move Here
</div>
{children}
Expand Down
21 changes: 12 additions & 9 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { FC, useEffect, useRef, useState } from 'react';
import ReactDOM from 'react-dom';
import { Draggable } from './components/Draggable';
import styles from './style/style.module.css';
import * as styles from './style/styles';

interface Props {
isOpen: boolean;
Expand All @@ -23,10 +23,6 @@ const Modal: FC<Props> = ({
const divRef = useRef<HTMLElement | null>(null);
const [divCreated, setDivCreated] = useState<Boolean>(false);

const overlayStyle = {
backgroundColor: isOverlay ? 'rgba(0, 0, 0, 0.5)' : 'transparent',
};

useEffect(() => {
if (isOpen) {
bodyRef.current = document.querySelector('body');
Expand All @@ -47,21 +43,28 @@ const Modal: FC<Props> = ({
divRef.current?.remove();
};

const overlayStyle = {
...styles.ReactPortalDialog__Overlay,
backgroundColor: isOverlay ? 'rgba(0, 0, 0, 0.5)' : 'transparent',
};

const bodyStyle = customStyles || styles.ReactPortalDialog__Body;

return divCreated && divRef.current
? ReactDOM.createPortal(
<>
<div
style={overlayStyle}
className={styles.ReactPortalDialog__Overlay}
className="ReactPortalDialog__Overlay"
onClick={closeHandler}
/>

{isDraggable ? (
<Draggable>{children}</Draggable>
<Draggable bodyStyle={bodyStyle}>{children}</Draggable>
) : (
<div
style={customStyles}
className={styles.ReactPortalDialog__Body}
style={bodyStyle}
className="ReactPortalDialog__Body"
>
{children}
</div>
Expand Down
23 changes: 0 additions & 23 deletions src/style/style.module.css

This file was deleted.

25 changes: 25 additions & 0 deletions src/style/styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';

export const ReactPortalDialog__Overlay: React.CSSProperties = {
position: 'fixed',
inset: '0',
zIndex: '1000',
};

export const ReactPortalDialog__Body: React.CSSProperties = {
position: 'fixed',
top: '50%',
left: '50%',
maxHeight: '200px',
width: '400px',
backgroundColor: 'rgb(223, 221, 221)',
zIndex: '1000',
transform: 'translate(-50%, -50%)',
};

export const ReactPortalDialog__Move: React.CSSProperties = {
backgroundColor: 'salmon',
textAlign: 'center',
padding: '5px',
cursor: 'move',
};

0 comments on commit 8207a9d

Please sign in to comment.