File size: 1,365 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import React, { useMemo } from 'react';
import { Button, Flex, Tooltip, Typography } from 'antd';

import useLocale from '../../../hooks/useLocale';
import ExternalLinkIcon from '../../icons/ExternalLinkIcon';
import Visualizer from './Visualizer';

export interface BezierVisualizerProps {
  value: string;
}

const RE = /^cubic-bezier\((.*)\)$/;

const locales = {
  cn: {
    open: '在 cubic-bezier.com 中打开',
  },
  en: {
    open: 'Open in cubic-bezier.com',
  },
};

const BezierVisualizer = (props: BezierVisualizerProps) => {
  const { value } = props;
  const [locale] = useLocale(locales);

  const controls = useMemo(() => {
    const m = RE.exec(value.toLowerCase().trim());
    if (m) {
      return m[1].split(',').map((v) => parseFloat(v.trim())) as [number, number, number, number];
    }
    return null;
  }, [value]);

  if (!controls) {
    return null;
  }

  return (
    <Flex vertical gap="small">
      <Visualizer controls={controls} />
      <Flex align="center">
        <Typography.Text>{value}</Typography.Text>
        <Tooltip title={locale.open}>
          <Button
            type="link"
            href={`https://cubic-bezier.com/#${controls.join(',')}`}
            target="_blank"
            icon={<ExternalLinkIcon />}
          />
        </Tooltip>
      </Flex>
    </Flex>
  );
};

export default BezierVisualizer;