How to use the @reach/rect.useRect function in @reach/rect

To help you get started, we’ve selected a few @reach/rect examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github reach / reach-ui / packages / tooltip / src / index.js View on Github external
DEBUG_STYLE
} = {}) {
  const id = useId(idProp);

  const [isVisible, setIsVisible] = useState(
    DEBUG_STYLE
      ? true
      : id === null
      ? false
      : context.id === id && state === VISIBLE
  );

  // hopefully they always pass a ref if they ever pass one
  const ownRef = useRef();
  const ref = useForkedRef(forwardedRef, ownRef);
  const triggerRect = useRect(ownRef, isVisible);

  useEffect(() => {
    return subscribe(() => {
      if (
        context.id === id &&
        (state === VISIBLE || state === LEAVING_VISIBLE)
      ) {
        setIsVisible(true);
      } else {
        setIsVisible(false);
      }
    });
  }, [id]);

  useEffect(() => checkStyles("tooltip"));
github reach / reach-ui / packages / tabs / examples / animated-bar.example.js View on Github external
function ExampleAnimatedTab(props) {
  const { isSelected } = props;

  // measure the size of our element, only listen to rect if active
  const ref = useRef();
  const rect = useRect(ref, isSelected);

  // get the style changing function from context
  const setActiveRect = useContext(AnimatedContext);

  // callup to set styles whenever we're active
  useLayoutEffect(() => {
    if (isSelected) {
      setActiveRect(rect);
    }
  }, [isSelected, rect, setActiveRect]);

  return (
    
  );
}
github lbryio / lbry-desktop / ui / component / common / tabs.jsx View on Github external
function Tab(props: TabProps) {
  // @reach/tabs provides an `isSelected` prop
  // We could also useContext to read it manually
  const { isSelected } = props;

  // Each tab measures itself
  const ref = useRef();
  const rect = useRect(ref, isSelected);

  // and calls up to the parent when it becomes selected
  // we useLayoutEffect to avoid flicker
  const setSelectedRect = useContext(AnimatedContext);
  useLayoutEffect(() => {
    if (isSelected) setSelectedRect(rect);
  }, [isSelected, rect, setSelectedRect]);

  return ;
}
github lbryio / lbry-desktop / ui / component / common / tabs.jsx View on Github external
function Tabs(props: TabsProps) {
  // Store the position of the selected Tab so we can animate the "active" bar to its position
  const [selectedRect, setSelectedRect] = useState(null);

  // Create a ref of the parent element so we can measure the relative "left" for the bar for the child Tab's
  const tabsRef = useRef();
  const tabsRect = useRect(tabsRef);

  const tabLabels = props.children[0];
  const tabContent = props.children[1];

  return (
    
      
        {tabLabels}

        <div style="{{"></div>
github reach / reach-ui / packages / rect / examples / use-rect.example.js View on Github external
export function Example() {
  const ref = useRef();
  const rect = useRect(ref);
  return (
    <div>
      <pre>{JSON.stringify(rect, null, 2)}</pre>
      <textarea>      &lt;span
        ref={ref}
        contentEditable
        dangerouslySetInnerHTML={{
          __html: "Observing my rect, I'm also editable"
        }}
        style={{
          display: "inline-block",
          padding: 10,
          margin: 10,
          border: "solid 1px",
          background: "#f0f0f0"</textarea></div>
github lbryio / lbry-desktop / ui / component / expandable / view.jsx View on Github external
export default function Expandable(props: Props) {
  const [expanded, setExpanded] = useState(false);
  const { children } = props;
  const ref = useRef();
  const rect = useRect(ref);

  function handleClick() {
    setExpanded(!expanded);
  }

  return (
    <div>
      {rect &amp;&amp; rect.height &gt; COLLAPSED_HEIGHT ? (
        <div>
          <div>
            {children}</div></div></div>
github reach / reach-ui / packages / tooltip / src / index.js View on Github external
{
    label,
    ariaLabel,
    position = positionDefault,
    isVisible,
    id,
    triggerRect,
    style,
    ...rest
  },
  forwardedRef
) {
  const useAriaLabel = ariaLabel != null;
  const ownRef = useRef(null);
  const ref = useForkedRef(forwardedRef, ownRef);
  const tooltipRect = useRect(ownRef, isVisible);
  return (
    
      <div style="{{" id="{useAriaLabel" role="{useAriaLabel" data-reach-tooltip="">
      {useAriaLabel &amp;&amp; (
        </div>
github reach / reach-ui / packages / popover / src / index.js View on Github external
const PopoverImpl = forwardRef(function PopoverImpl(
  { targetRef, position = positionDefault, style, ...rest },
  forwardedRef
) {
  const popoverRef = useRef();
  const popoverRect = useRect(popoverRef);
  const targetRect = useRect(targetRef);
  const ref = useForkedRef(popoverRef, forwardedRef);

  useSimulateTabNavigationForReactTree(targetRef, popoverRef);

  return (
    <div style="{{" data-reach-popover="">
  );</div>
github reach / reach-ui / packages / popover / src / index.js View on Github external
const PopoverImpl = forwardRef(function PopoverImpl(
  { targetRef, position = positionDefault, style, ...rest },
  forwardedRef
) {
  const popoverRef = useRef();
  const popoverRect = useRect(popoverRef);
  const targetRect = useRect(targetRef);
  const ref = useForkedRef(popoverRef, forwardedRef);

  useSimulateTabNavigationForReactTree(targetRef, popoverRef);

  return (
    <div style="{{" data-reach-popover=""></div>

@reach/rect

Measure React elements position in the DOM

MIT
Latest version published 2 years ago

Package Health Score

73 / 100
Full package analysis

Popular @reach/rect functions