Skip to content

Commit

Permalink
feat(@clayui/shared): simplify function
Browse files Browse the repository at this point in the history
  • Loading branch information
ambrinchaudhary committed May 19, 2021
1 parent b7445bc commit d326ace
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions packages/clay-shared/src/Portal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@ const ClayPortalContext = React.createContext<React.RefObject<Element | null> |

ClayPortalContext.displayName = 'ClayPortalContext';

const createElement = (
nodeName: string,
attributes: Record<string, string>
) => {
const element = document.createElement(nodeName);

Object.keys(attributes).forEach((key) => {
element.setAttribute(
key === 'className' ? 'class' : key,
attributes[key]
);
});
const createDivElement = (className?: string, id?: string) => {
const element = document.createElement('div');

if (className) {
element.setAttribute('class', className);
}

if (id) {
element.setAttribute('id', id);
}

return element;
};
Expand Down Expand Up @@ -54,16 +52,15 @@ interface IProps {

export const ClayPortal: React.FunctionComponent<IProps> = ({
children,
className,
containerRef,
id,
subPortalRef,
...otherProps
}) => {
const parentPortalRef = React.useContext(ClayPortalContext);

const portalRef = React.useRef(
typeof document !== 'undefined'
? createElement('div', otherProps as Record<string, string>)
: null
typeof document !== 'undefined' ? createDivElement(className, id) : null
);

React.useEffect(() => {
Expand Down

0 comments on commit d326ace

Please sign in to comment.