Repository navigation
Description
Description
Currently, <ContentEditable /> creates a ref internally that references the base element it returns. By allowing users access to that ref, we can use the editor as a base for additional functionality composition without resorting to findDOMNode.
The specific use case this would address is wrapping the editor in Ant Design's Dropdown, Popover and Tooltip components. The Dropdown component is especially useful for generating contextual menus for the editor that retain the same design as other Ant components.
These components look for a ref argument in the child component function/constructor. If they can't find a ref argument, they use findDOMNode, which is deprecated in StrictMode.
I've tried to wrap <ContentEditable /> in a component with its own ref that simply returns <ContentEditable />, then tried to use document.querySelector<HTMLElement>(`#${id}.${className}`) inside useImperativeRef to reference the editable div. This wasn't successful. Although I could wrap <ContentEditable /> in another div, this complicates layout styling for the editable div, leaving a less-than-ideal DX.
Impact
This would benefit users of Ant Design who want to create context menus with this library. I don't have exact numbers, but I remember reading that it's the second-most-used design library in the ecosystem, so I imagine it will benefit a decent number of people.
Probably the main concern is whether or not this feels like it's exposing internals. I don't think so, since someone who wants to directly access the container could class it or wrap it with a reffed div and go to town, but it's not my call.
Proposed Solution
It should be fairly straightforward to look for an incoming ref and update it at the same time as the current callback (from src):
const ref = useCallback(
(rootElement: null | HTMLElement) => {
if (userProvidedRef) {
if (typeof userProvidedRef === 'function') {
userProvidedRef(rootElement);
} else {
(userProvidedRef as MutableRefObject(HTMLElement | null).current = rootElement;
}
}
// defaultView is required for a root element.
// In multi-window setups, the defaultView may not exist at certain points.
if (
rootElement &&
rootElement.ownerDocument &&
rootElement.ownerDocument.defaultView
) {
editor.setRootElement(rootElement);
} else {
editor.setRootElement(null);
}
},
[editor, userProvidedRef],
);I'd be happy to submit this in a PR if approved.
Activity
[-]Feature: Expose <ContentEditable /> HTMLDiveElement ref[/-][+]Feature: Expose <ContentEditable /> HTMLElement ref[/+]etrepum commentedon Jul 14, 2024
I think there is already a PR for this #6381
etrepum commentedon Jul 14, 2024
ContentEditable doesn't really do anything special, you don't really lose compatibility with anything else if you copy and modify its implementation to behave however you want. At the end of the day it's just rendering a div with a ref that calls editor.setRootElement, plus some extra code to handle a placeholder state.
landisdesign commentedon Jul 14, 2024
Ahh excellent! I didn't see anything when searching issues. I'll close this and look forward to the update. Thanks!
landisdesign commentedon Jul 14, 2024
Fair, but the less I can fork from the reference implementation, the better. Sounds like this is working itself out. Thanks again.
etrepum commentedon Jul 14, 2024
Of course, it's usually better to have less code to maintain, I only mentioned that in case you were blocked and wanted to ship something sooner than the next release and didn't want to depend on a nightly after it's merged. Since the PR is straightforward and comes from a core maintainer at Meta I suspect it will be merged early next week. The release cadence is usually every ~3-8 weeks. Meta doesn't internally depend on npm releases, so that's all driven by other stakeholders.
landisdesign commentedon Jul 14, 2024
Sounds good. Thanks for the context!