Skip to content

Commit d326ace

Browse files
feat(@clayui/shared): simplify function
1 parent b7445bc commit d326ace

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

packages/clay-shared/src/Portal.tsx

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,16 @@ const ClayPortalContext = React.createContext<React.RefObject<Element | null> |
1212

1313
ClayPortalContext.displayName = 'ClayPortalContext';
1414

15-
const createElement = (
16-
nodeName: string,
17-
attributes: Record<string, string>
18-
) => {
19-
const element = document.createElement(nodeName);
20-
21-
Object.keys(attributes).forEach((key) => {
22-
element.setAttribute(
23-
key === 'className' ? 'class' : key,
24-
attributes[key]
25-
);
26-
});
15+
const createDivElement = (className?: string, id?: string) => {
16+
const element = document.createElement('div');
17+
18+
if (className) {
19+
element.setAttribute('class', className);
20+
}
21+
22+
if (id) {
23+
element.setAttribute('id', id);
24+
}
2725

2826
return element;
2927
};
@@ -54,16 +52,15 @@ interface IProps {
5452

5553
export const ClayPortal: React.FunctionComponent<IProps> = ({
5654
children,
55+
className,
5756
containerRef,
57+
id,
5858
subPortalRef,
59-
...otherProps
6059
}) => {
6160
const parentPortalRef = React.useContext(ClayPortalContext);
6261

6362
const portalRef = React.useRef(
64-
typeof document !== 'undefined'
65-
? createElement('div', otherProps as Record<string, string>)
66-
: null
63+
typeof document !== 'undefined' ? createDivElement(className, id) : null
6764
);
6865

6966
React.useEffect(() => {

0 commit comments

Comments
 (0)