廓形仪rn版本-技术调研
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.4 KiB

  1. 'use client';
  2. import React from 'react';
  3. import { createTooltip } from '@gluestack-ui/tooltip';
  4. import { View, Text, ViewStyle } from 'react-native';
  5. import type { VariantProps } from '@gluestack-ui/nativewind-utils';
  6. import { tva } from '@gluestack-ui/nativewind-utils/tva';
  7. import { withStyleContext } from '@gluestack-ui/nativewind-utils/withStyleContext';
  8. import {
  9. Motion,
  10. AnimatePresence,
  11. MotionComponentProps,
  12. } from '@legendapp/motion';
  13. import { cssInterop } from 'nativewind';
  14. type IMotionViewProps = React.ComponentProps<typeof View> &
  15. MotionComponentProps<typeof View, ViewStyle, unknown, unknown, unknown>;
  16. const MotionView = Motion.View as React.ComponentType<IMotionViewProps>;
  17. export const UITooltip = createTooltip({
  18. Root: withStyleContext(View),
  19. Content: MotionView,
  20. Text: Text,
  21. AnimatePresence: AnimatePresence,
  22. });
  23. cssInterop(MotionView, { className: 'style' });
  24. const tooltipStyle = tva({
  25. base: 'w-full h-full web:pointer-events-none',
  26. });
  27. const tooltipContentStyle = tva({
  28. base: 'py-1 px-3 rounded-sm bg-background-900 web:pointer-events-auto',
  29. });
  30. const tooltipTextStyle = tva({
  31. base: 'font-normal tracking-normal web:select-none text-xs text-typography-50',
  32. variants: {
  33. isTruncated: {
  34. true: {
  35. props: 'line-clamp-1 truncate',
  36. },
  37. },
  38. bold: {
  39. true: 'font-bold',
  40. },
  41. underline: {
  42. true: 'underline',
  43. },
  44. strikeThrough: {
  45. true: 'line-through',
  46. },
  47. size: {
  48. '2xs': 'text-2xs',
  49. 'xs': 'text-xs',
  50. 'sm': 'text-sm',
  51. 'md': 'text-base',
  52. 'lg': 'text-lg',
  53. 'xl': 'text-xl',
  54. '2xl': 'text-2xl',
  55. '3xl': 'text-3xl',
  56. '4xl': 'text-4xl',
  57. '5xl': 'text-5xl',
  58. '6xl': 'text-6xl',
  59. },
  60. sub: {
  61. true: 'text-xs',
  62. },
  63. italic: {
  64. true: 'italic',
  65. },
  66. highlight: {
  67. true: 'bg-yellow-500',
  68. },
  69. },
  70. });
  71. type ITooltipProps = React.ComponentProps<typeof UITooltip> &
  72. VariantProps<typeof tooltipStyle> & { className?: string };
  73. type ITooltipContentProps = React.ComponentProps<typeof UITooltip.Content> &
  74. VariantProps<typeof tooltipContentStyle> & { className?: string };
  75. type ITooltipTextProps = React.ComponentProps<typeof UITooltip.Text> &
  76. VariantProps<typeof tooltipTextStyle> & { className?: string };
  77. const Tooltip = React.forwardRef<
  78. React.ComponentRef<typeof UITooltip>,
  79. ITooltipProps
  80. >(function Tooltip({ className, ...props }, ref) {
  81. return (
  82. <UITooltip
  83. ref={ref}
  84. className={tooltipStyle({ class: className })}
  85. {...props}
  86. />
  87. );
  88. });
  89. const TooltipContent = React.forwardRef<
  90. React.ComponentRef<typeof UITooltip.Content>,
  91. ITooltipContentProps & { className?: string }
  92. >(function TooltipContent({ className, ...props }, ref) {
  93. return (
  94. <UITooltip.Content
  95. ref={ref}
  96. {...props}
  97. className={tooltipContentStyle({
  98. class: className,
  99. })}
  100. pointerEvents="auto"
  101. />
  102. );
  103. });
  104. const TooltipText = React.forwardRef<
  105. React.ComponentRef<typeof UITooltip.Text>,
  106. ITooltipTextProps & { className?: string }
  107. >(function TooltipText({ size, className, ...props }, ref) {
  108. return (
  109. <UITooltip.Text
  110. ref={ref}
  111. className={tooltipTextStyle({ size, class: className })}
  112. {...props}
  113. />
  114. );
  115. });
  116. Tooltip.displayName = 'Tooltip';
  117. TooltipContent.displayName = 'TooltipContent';
  118. TooltipText.displayName = 'TooltipText';
  119. export { Tooltip, TooltipContent, TooltipText };