Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request to support optionally passing window as a param #123

Open
jordanwilliams opened this issue Sep 18, 2024 · 0 comments
Open

Request to support optionally passing window as a param #123

jordanwilliams opened this issue Sep 18, 2024 · 0 comments

Comments

@jordanwilliams
Copy link

👋🏾 Hello again! After getting around the react-dnd issue I described in #106, I was able to use pdnd to support drag and drop in some feature code here at Slack 🚀 . Overall it was pretty smooth, though I'm reaching out with a request that will help me release the change. The request is to optionally support passing window as a param.

For context, the Slack desktop client supports multiple windows. Drag and drop works great in our "main" window, though it doesn't work if you open a view in a separate "child" window. We manage child windows from the main window, and we cannot access window or window.document directly. We instead pull the window from React context (e.g. const win = useWindow()) to ensure we're using the correct window regardless of where a view is rendered. There are some uses of window/document inside of pdnd that I'm hoping can be updated to use a provided window param instead if it is passed. For example, react-dnd supports passing the window as the context param to their DndProvider (source). Another example would be downshift which supports passing the window as an environment param (docs).

// An example of our current usage of react-dnd
function ExampleComponent() {
	const win = useWindow();

	return (
		<DndProvider backend={HTML5Backend} context={win}>
			{children}
		</DndProvider>
	);
}

Below is an example of how this could potentially look in pdnd

// An example of passing window to pdnd in the future
function ExampleComponent() {
	const ref = useRef<HTMLDivElement | null>(null);
	const win = useWindow();

	useEffect(() => {
		const element = ref.current;
		if (!element) return;

		return combine(
			draggable({
				element,
				environment: win,
				// ... other params
			}),
			dropTargetForElements({
				element,
				environment: win,
				// ... other params
			}),
		);
	}, [win]);

	return <div ref={ref}>Drag me</div>;
}

I've only briefly looked at the pdnd source code and saw that there might be a chance to thread it through draggable to adapater.registerUsage and ultimately to mount. But I don't want to oversimplify the changes, as I'm not aware of all the places that access window or document. Let me know if this is something you all would be open to! It would be a massive help to us in our effort to adopt pdnd here at Slack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant