<commit_before>import React from 'react';
import { useToggleState } from '@react-stately/toggle';
import { StyleSheet, ViewStyle, Switch as RNSwitch } from 'react-native';
import styled from 'styled-components/native';
import isNil from 'lodash.isnil';
import { usePropsResolution } from '../../../hooks/useThemeProps';
import { useToken } from '../../../hooks';
import { border, color, flexbox, layout, space, position } from 'styled-system';
import {
  customBorder,
  customBackground,
  customOutline,
  customLayout,
  customExtra,
  customShadow,
  customPosition,
} from '../../../utils/customProps';
import type { ISwitchProps } from './types';
import { mergeRefs } from '../../../utils';
import { useHover } from '@react-native-aria/interactions';

const StyledNBSwitch = styled(RNSwitch)<ISwitchProps>(
  color,
  space,
  layout,
  flexbox,
  border,
  position,
  customPosition,
  customBorder,
  customBackground,
  customOutline,
  customShadow,
  customExtra,
  customLayout
);

const Switch = (
  {
    style,
    onToggle,
    isDisabled,
    isInvalid,
    isChecked,
    defaultIsChecked,
    accessibilityLabel,
    accessibilityHint,
    ...props
  }: ISwitchProps,
  ref: any
) => {
  const state = useToggleState({
    defaultSelected: !isNil(defaultIsChecked) ? defaultIsChecked : false,
  });
  const {
    onTrackColor: _onTrackColor,
    offTrackColor: _offTrackColor,
    onThumbColor: _onThumbColor,
    offThumbColor: _offThumbColor,
    _hover,
    ...newProps
  } = usePropsResolution('Switch', props);
  const borderColorInvalid = useToken('colors', 'danger.600');
  const checked = !isNil(isChecked) ? isChecked : state.isSelected;
  const onTrackColor = useToken('colors', _onTrackColor);
  const offTrackColor = useToken('colors', _offTrackColor);
  const onThumbColor = useToken('colors', _onThumbColor);
  const offThumbColor = useToken('colors', _offThumbColor);
  const inValidPropFactors = {
    borderWidth: 1,
    borderRadius: 16,
    borderColor: borderColorInvalid,
  };

  let computedStyle: ViewStyle = StyleSheet.flatten([
    style,
    { transform: newProps.transform ?? undefined },
    isInvalid ? inValidPropFactors : {},
  ]);

  const _ref = React.useRef(null);
  const { isHovered } = useHover({}, _ref);

  return (
    <StyledNBSwitch
      accessibilityLabel={accessibilityLabel}
      accessibilityHint={accessibilityHint}
      trackColor={{ false: offTrackColor, true: onTrackColor }}
      thumbColor={checked ? onThumbColor : offThumbColor}
      activeThumbColor={onThumbColor} // react-native-web prop for active thumbColor
      ios_backgroundColor={offTrackColor}
      {...(isHovered && _hover)}
      {...newProps}
      disabled={isDisabled}
      onValueChange={onToggle ? onToggle : state.toggle}
      value={checked}
      style={computedStyle}
      ref={mergeRefs([ref, _ref])}
      opacity={isDisabled ? 0.4 : 1}
    />
  );
};

export default React.memo(React.forwardRef(Switch));
export type { ISwitchProps };<commit_msg>chore: switch docs<commit_after>import React, { memo, forwardRef } from 'react';
import { useToggleState } from '@react-stately/toggle';
import { StyleSheet, ViewStyle, Switch as RNSwitch } from 'react-native';
import styled from 'styled-components/native';
import isNil from 'lodash.isnil';
import { usePropsResolution } from '../../../hooks/useThemeProps';
import { useToken } from '../../../hooks';
import { border, color, flexbox, layout, space, position } from 'styled-system';
import {
  customBorder,
  customBackground,
  customOutline,
  customLayout,
  customExtra,
  customShadow,
  customPosition,
} from '../../../utils/customProps';
import type { ISwitchProps } from './types';
import { mergeRefs } from '../../../utils';
import { useHover } from '@react-native-aria/interactions';

const StyledNBSwitch = styled(RNSwitch)<ISwitchProps>(
  color,
  space,
  layout,
  flexbox,
  border,
  position,
  customPosition,
  customBorder,
  customBackground,
  customOutline,
  customShadow,
  customExtra,
  customLayout
);

const Switch = (
  {
    style,
    onToggle,
    isDisabled,
    isInvalid,
    isChecked,
    defaultIsChecked,
    accessibilityLabel,
    accessibilityHint,
    ...props
  }: ISwitchProps,
  ref: any
) => {
  const state = useToggleState({
    defaultSelected: !isNil(defaultIsChecked) ? defaultIsChecked : false,
  });
  const {
    onTrackColor: _onTrackColor,
    offTrackColor: _offTrackColor,
    onThumbColor: _onThumbColor,
    offThumbColor: _offThumbColor,
    _hover,
    ...newProps
  } = usePropsResolution('Switch', props);
  const borderColorInvalid = useToken('colors', 'danger.600');
  const checked = !isNil(isChecked) ? isChecked : state.isSelected;
  const onTrackColor = useToken('colors', _onTrackColor);
  const offTrackColor = useToken('colors', _offTrackColor);
  const onThumbColor = useToken('colors', _onThumbColor);
  const offThumbColor = useToken('colors', _offThumbColor);
  const inValidPropFactors = {
    borderWidth: 1,
    borderRadius: 16,
    borderColor: borderColorInvalid,
  };

  let computedStyle: ViewStyle = StyleSheet.flatten([
    style,
    { transform: newProps.transform ?? undefined },
    isInvalid ? inValidPropFactors : {},
  ]);

  const _ref = React.useRef(null);
  const { isHovered } = useHover({}, _ref);

  return (
    <StyledNBSwitch
      accessibilityLabel={accessibilityLabel}
      accessibilityHint={accessibilityHint}
      trackColor={{ false: offTrackColor, true: onTrackColor }}
      thumbColor={checked ? onThumbColor : offThumbColor}
      activeThumbColor={onThumbColor} // react-native-web prop for active thumbColor
      ios_backgroundColor={offTrackColor}
      {...(isHovered && _hover)}
      {...newProps}
      disabled={isDisabled}
      onValueChange={onToggle ? onToggle : state.toggle}
      value={checked}
      style={computedStyle}
      ref={mergeRefs([ref, _ref])}
      opacity={isDisabled ? 0.4 : 1}
    />
  );
};

export default memo(forwardRef(Switch));
export type { ISwitchProps };
{'fraction_non_alphanumeric': 0.1266810559521833, 'fraction_numerical': 0.0029885439149925285, 'mean_word_length': 3.854149879129734, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '5889239', 'n_tokens_mistral': 1884, 'n_tokens_neox': 1863, 'n_words': 410}