Skip to content

Feature: Expose <ContentEditable /> HTMLElement ref #6399

Closed

Description

@landisdesign

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

  1. added
    enhancement
    on Jul 13, 2024
  2. changed the title [-]Feature: Expose <ContentEditable /> HTMLDiveElement ref[/-] [+]Feature: Expose <ContentEditable /> HTMLElement ref[/+] on Jul 13, 2024
  3. etrepum commented on Jul 14, 2024

    @etrepum
    Collaborator

    I think there is already a PR for this #6381

  4. etrepum commented on Jul 14, 2024

    @etrepum
    Collaborator

    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.

  5. landisdesign commented on Jul 14, 2024

    @landisdesign
    ContributorAuthor

    Ahh excellent! I didn't see anything when searching issues. I'll close this and look forward to the update. Thanks!

  6. landisdesign commented on Jul 14, 2024

    @landisdesign
    ContributorAuthor

    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.

    Fair, but the less I can fork from the reference implementation, the better. Sounds like this is working itself out. Thanks again.

  7. etrepum commented on Jul 14, 2024

    @etrepum
    Collaborator

    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.

  8. landisdesign commented on Jul 14, 2024

    @landisdesign
    ContributorAuthor

    Sounds good. Thanks for the context!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions