<commit_before>import React, {
  forwardRef,
  useContext,
  useEffect,
  useMemo,
  useRef,
} from 'react';
import styled, { ThemeContext } from 'styled-components';
import { defaultProps } from '../../default-props';

import { FocusedContainer } from '../FocusedContainer';
import { Keyboard } from '../Keyboard';
import { ResponsiveContext } from '../../contexts/ResponsiveContext';
import { OptionsContext } from '../../contexts/OptionsContext';
import { ContainerTargetContext } from '../../contexts/ContainerTargetContext';
import { useAnalytics } from '../../contexts/AnalyticsContext';

import {
  backgroundIsDark,
  findVisibleParent,
  PortalContext,
} from '../../utils';

import { StyledLayer, StyledContainer, StyledOverlay } from './StyledLayer';

const HiddenAnchor = styled.a`
  width: 0;
  height: 0;
  overflow: hidden;
  position: absolute;
`;

const defaultPortalContext = [];

const LayerContainer = forwardRef(
  (
    {
      background,
      children,
      full = false,
      id,
      margin = 'none',
      modal = true,
      onClickOutside,
      onEsc,
      plain,
      position = 'center',
      responsive = true,
      target: layerTarget,
      ...rest
    },
    ref,
  ) => {
    const containerTarget = useContext(ContainerTargetContext);
    const theme = useContext(ThemeContext) || defaultProps.theme;
    const size = useContext(ResponsiveContext);
    // layerOptions was created to preserve backwards compatibility but
    // should not be supported in v3
    const { layer: layerOptions } = useContext(OptionsContext);
    const anchorRef = useRef();
    const containerRef = useRef();
    const layerRef = useRef();
    const portalContext = useContext(PortalContext) || defaultPortalContext;
    const portalId = useMemo(() => portalContext.length, [portalContext]);
    const nextPortalContext = useMemo(
      () => [...portalContext, portalId],
      [portalContext, portalId],
    );

    const sendAnalytics = useAnalytics();

    useEffect(() => {
      const start = new Date();
      const element = layerRef.current;
      const isHidden = position === 'hidden';
      if (!isHidden) {
        sendAnalytics({
          type: 'layerOpen',
          element,
        });
      }
      return () => {
        if (!isHidden) {
          sendAnalytics({
            type: 'layerClose',
            element,
            elapsed: new Date().getTime() - start.getTime(),
          });
        }
      };
    }, [sendAnalytics, layerRef, position]);

    useEffect(() => {
      if (position !== 'hidden') {
        const node = layerRef.current || containerRef.current || ref.current;
        if (node && node.scrollIntoView) node.scrollIntoView();
        // Once layer is open we make sure it has focus so that you
        // can start tabbing inside the layer. If the caller put focus
        // on an element already, we honor that. Otherwise, we put
        // the focus in the hidden anchor.
        let element = document.activeElement;
        while (element) {
          if (element === containerRef.current) {
            // already have focus inside the container
            break;
          }
          element = element.parentElement;
        }
        if (modal && !element && anchorRef.current) {
          anchorRef.current.focus();
        }
      }
    }, [modal, position, ref]);

    useEffect(() => {
      if (position !== 'hidden') {
        const node = layerRef.current || containerRef.current || ref.current;
        if (node && node.scrollIntoView) node.scrollIntoView();
      }
    }, [position, ref]);

    useEffect(() => {
      const onClickDocument = (event) => {
        // determine which portal id the target is in, if any
        let clickedPortalId = null;
        let node =
          containerTarget === document.body ? event.target : event?.path[0];<commit_msg>Fix `event.path` deprecated on Layer onClickOutside (#6457)<commit_after>import React, {
  forwardRef,
  useContext,
  useEffect,
  useMemo,
  useRef,
} from 'react';
import styled, { ThemeContext } from 'styled-components';
import { defaultProps } from '../../default-props';

import { FocusedContainer } from '../FocusedContainer';
import { Keyboard } from '../Keyboard';
import { ResponsiveContext } from '../../contexts/ResponsiveContext';
import { OptionsContext } from '../../contexts/OptionsContext';
import { ContainerTargetContext } from '../../contexts/ContainerTargetContext';
import { useAnalytics } from '../../contexts/AnalyticsContext';

import {
  backgroundIsDark,
  findVisibleParent,
  PortalContext,
} from '../../utils';

import { StyledLayer, StyledContainer, StyledOverlay } from './StyledLayer';

const HiddenAnchor = styled.a`
  width: 0;
  height: 0;
  overflow: hidden;
  position: absolute;
`;

const defaultPortalContext = [];

const LayerContainer = forwardRef(
  (
    {
      background,
      children,
      full = false,
      id,
      margin = 'none',
      modal = true,
      onClickOutside,
      onEsc,
      plain,
      position = 'center',
      responsive = true,
      target: layerTarget,
      ...rest
    },
    ref,
  ) => {
    const containerTarget = useContext(ContainerTargetContext);
    const theme = useContext(ThemeContext) || defaultProps.theme;
    const size = useContext(ResponsiveContext);
    // layerOptions was created to preserve backwards compatibility but
    // should not be supported in v3
    const { layer: layerOptions } = useContext(OptionsContext);
    const anchorRef = useRef();
    const containerRef = useRef();
    const layerRef = useRef();
    const portalContext = useContext(PortalContext) || defaultPortalContext;
    const portalId = useMemo(() => portalContext.length, [portalContext]);
    const nextPortalContext = useMemo(
      () => [...portalContext, portalId],
      [portalContext, portalId],
    );

    const sendAnalytics = useAnalytics();

    useEffect(() => {
      const start = new Date();
      const element = layerRef.current;
      const isHidden = position === 'hidden';
      if (!isHidden) {
        sendAnalytics({
          type: 'layerOpen',
          element,
        });
      }
      return () => {
        if (!isHidden) {
          sendAnalytics({
            type: 'layerClose',
            element,
            elapsed: new Date().getTime() - start.getTime(),
          });
        }
      };
    }, [sendAnalytics, layerRef, position]);

    useEffect(() => {
      if (position !== 'hidden') {
        const node = layerRef.current || containerRef.current || ref.current;
        if (node && node.scrollIntoView) node.scrollIntoView();
        // Once layer is open we make sure it has focus so that you
        // can start tabbing inside the layer. If the caller put focus
        // on an element already, we honor that. Otherwise, we put
        // the focus in the hidden anchor.
        let element = document.activeElement;
        while (element) {
          if (element === containerRef.current) {
            // already have focus inside the container
            break;
          }
          element = element.parentElement;
        }
        if (modal && !element && anchorRef.current) {
          anchorRef.current.focus();
        }
      }
    }, [modal, position, ref]);

    useEffect(() => {
      if (position !== 'hidden') {
        const node = layerRef.current || containerRef.current || ref.current;
        if (node && node.scrollIntoView) node.scrollIntoView();
      }
    }, [position, ref]);

    useEffect(() => {
      const onClickDocument = (event) => {
        // determine which portal id the target is in, if any
        let clickedPortalId = null;
        let node =
          containerTarget === document.body
            ? event.target
            : event?.composedPath()[0];

{'fraction_non_alphanumeric': 0.12623476587556126, 'fraction_numerical': 0.0015394483643361128, 'mean_word_length': 2.4148050810337276, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '661590', 'n_tokens_mistral': 2104, 'n_tokens_neox': 2090, 'n_words': 593}