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

o-tooltip's imperative interface could accept an HTMLElement node #1609

Open
aendra-rininsland opened this issue Apr 26, 2024 · 0 comments
Open

Comments

@aendra-rininsland
Copy link
Member

One difficulty I often have with o-tooltip is that it requires an ID string for the target, which are often in short supply around React-land. Looking at the code, the imperative interface only uses the opt.target value to run a getElementById to then pass on the returned node to the constructor.

Allowing opts.target to be an HTMLElement allows for patterns like the following (in React pseudocode):

import React, { useEffect } from 'react';
import OTooltip from '@financial-times/o-tooltip';

const Tooltip = ({target, ...opts}) => {
    const tooltip = useRef(null);
    const el =  useRef(null);

    useEffect(() => {
        tooltip.current = new OTooltip(el.current, {target, ...opts});
    }, []);

    return <div ref={el} />
}

...Where the target prop is likely another ref passed down from a parent element:

const ButtonWithTooltip = (props) => {
  const ref = useRef(null);
  return <>
    <button ref={ref}>Some content to tooltip</button>
    <Tooltip target={ref} {...props} />
  </>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Backlog
Development

No branches or pull requests

1 participant