File size: 3,899 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
{
  "type": "composition",
  "npmDependencies": [
    "react-icons"
  ],
  "fileDependencies": [],
  "id": "password-input",
  "file": {
    "name": "password-input.tsx",
    "content": "\"use client\"\n\nimport type {\n  ButtonProps,\n  GroupProps,\n  InputProps,\n  StackProps,\n} from \"@chakra-ui/react\"\nimport {\n  Box,\n  HStack,\n  IconButton,\n  Input,\n  InputGroup,\n  Stack,\n  mergeRefs,\n  useControllableState,\n} from \"@chakra-ui/react\"\nimport * as React from \"react\"\nimport { LuEye, LuEyeOff } from \"react-icons/lu\"\n\nexport interface PasswordVisibilityProps {\n  defaultVisible?: boolean\n  visible?: boolean\n  onVisibleChange?: (visible: boolean) => void\n  visibilityIcon?: { on: React.ReactNode; off: React.ReactNode }\n}\n\nexport interface PasswordInputProps\n  extends InputProps,\n    PasswordVisibilityProps {\n  rootProps?: GroupProps\n}\n\nexport const PasswordInput = React.forwardRef<\n  HTMLInputElement,\n  PasswordInputProps\n>(function PasswordInput(props, ref) {\n  const {\n    rootProps,\n    defaultVisible,\n    visible: visibleProp,\n    onVisibleChange,\n    visibilityIcon = { on: <LuEye />, off: <LuEyeOff /> },\n    ...rest\n  } = props\n\n  const [visible, setVisible] = useControllableState({\n    value: visibleProp,\n    defaultValue: defaultVisible || false,\n    onChange: onVisibleChange,\n  })\n\n  const inputRef = React.useRef<HTMLInputElement>(null)\n\n  return (\n    <InputGroup\n      endElement={\n        <VisibilityTrigger\n          disabled={rest.disabled}\n          onPointerDown={(e) => {\n            if (rest.disabled) return\n            if (e.button !== 0) return\n            e.preventDefault()\n            setVisible(!visible)\n          }}\n        >\n          {visible ? visibilityIcon.off : visibilityIcon.on}\n        </VisibilityTrigger>\n      }\n      {...rootProps}\n    >\n      <Input\n        {...rest}\n        ref={mergeRefs(ref, inputRef)}\n        type={visible ? \"text\" : \"password\"}\n      />\n    </InputGroup>\n  )\n})\n\nconst VisibilityTrigger = React.forwardRef<HTMLButtonElement, ButtonProps>(\n  function VisibilityTrigger(props, ref) {\n    return (\n      <IconButton\n        tabIndex={-1}\n        ref={ref}\n        me=\"-2\"\n        aspectRatio=\"square\"\n        size=\"sm\"\n        variant=\"ghost\"\n        height=\"calc(100% - {spacing.2})\"\n        aria-label=\"Toggle password visibility\"\n        {...props}\n      />\n    )\n  },\n)\n\ninterface PasswordStrengthMeterProps extends StackProps {\n  max?: number\n  value: number\n}\n\nexport const PasswordStrengthMeter = React.forwardRef<\n  HTMLDivElement,\n  PasswordStrengthMeterProps\n>(function PasswordStrengthMeter(props, ref) {\n  const { max = 4, value, ...rest } = props\n\n  const percent = (value / max) * 100\n  const { label, colorPalette } = getColorPalette(percent)\n\n  return (\n    <Stack align=\"flex-end\" gap=\"1\" ref={ref} {...rest}>\n      <HStack width=\"full\" ref={ref} {...rest}>\n        {Array.from({ length: max }).map((_, index) => (\n          <Box\n            key={index}\n            height=\"1\"\n            flex=\"1\"\n            rounded=\"sm\"\n            data-selected={index < value ? \"\" : undefined}\n            layerStyle=\"fill.subtle\"\n            colorPalette=\"gray\"\n            _selected={{\n              colorPalette,\n              layerStyle: \"fill.solid\",\n            }}\n          />\n        ))}\n      </HStack>\n      {label && <HStack textStyle=\"xs\">{label}</HStack>}\n    </Stack>\n  )\n})\n\nfunction getColorPalette(percent: number) {\n  switch (true) {\n    case percent < 33:\n      return { label: \"Low\", colorPalette: \"red\" }\n    case percent < 66:\n      return { label: \"Medium\", colorPalette: \"orange\" }\n    default:\n      return { label: \"High\", colorPalette: \"green\" }\n  }\n}\n"
  },
  "component": "PasswordInput"
}