Skip to content

Commit

Permalink
add Dynamic Portal
Browse files Browse the repository at this point in the history
  • Loading branch information
enszrlu committed Sep 19, 2024
1 parent 0ee6b1f commit 8868796
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 86 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"start": "npm run build && node dist/index.js"
},
"peerDependencies": {
"@radix-ui/react-portal": ">=1.1.1",
"framer-motion": ">=11",
"next": ">=13",
"react": ">=18",
Expand Down
85 changes: 3 additions & 82 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions src/DynamicPortal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useEffect, useState } from 'react';
import type { ReactNode } from 'react';
import { createPortal } from 'react-dom';

interface DynamicPortalProps {
wrapperID?: string;
children: ReactNode;
}

const DynamicPortal: React.FC<DynamicPortalProps> = ({ wrapperID, children }) => {
const [wrapperElement, setWrapperElement] = useState<HTMLElement | null>(null);

useEffect(() => {
// If wrapperID is provided, attempt to find the corresponding element
if (wrapperID) {
const targetElement = document.getElementById(wrapperID);
if (targetElement) {
setWrapperElement(targetElement);
} else {
console.warn(
`Element with ID "${wrapperID}" not found. Rendering into document body.`,
);
setWrapperElement(document.body); // Default to body if element is not found
}
} else {
// If no wrapperID is provided, default to rendering in the body
setWrapperElement(document.body);
}
}, [wrapperID]);

if (!wrapperElement) {
return null; // Return null while wrapperElement is being determined
}

return createPortal(children, wrapperElement);
};

export default DynamicPortal;
6 changes: 3 additions & 3 deletions src/NextStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import React, { useState, useEffect, useRef, useCallback } from 'react';
import { useNextStep } from './NextStepContext';
import { motion, useInView } from 'framer-motion';
import { useRouter } from 'next/navigation';
import { Portal } from '@radix-ui/react-portal';

// Types
import { NextStepProps } from './types';
import DefaultCard from './DefaultCard';
import DynamicPortal from './DynamicPortal';

const NextStep: React.FC<NextStepProps> = ({
children,
Expand Down Expand Up @@ -650,7 +650,7 @@ const NextStep: React.FC<NextStepProps> = ({

{/* NextStep Overlay Step Content */}
{pointerPosition && isNextStepVisible && (
<Portal>
<DynamicPortal wrapperID={currentTourSteps?.[currentStep]?.wrapperID}>
<motion.div
data-name="nextstep-overlay"
className="absolute inset-0 overflow-hidden"
Expand Down Expand Up @@ -787,7 +787,7 @@ const NextStep: React.FC<NextStepProps> = ({
</motion.div>
</motion.div>
</motion.div>
</Portal>
</DynamicPortal>
)}
</div>
);
Expand Down
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export interface Step {
// Routing
nextRoute?: string;
prevRoute?: string;
// Dynamic Portal
wrapperID?: string;
}

// Tour
Expand Down

0 comments on commit 8868796

Please sign in to comment.