|
| 1 | +import React, { useState } from "react"; |
| 2 | +import dynamic from "next/dynamic"; |
| 3 | + |
| 4 | +// Toast |
| 5 | +import toast from "react-hot-toast"; |
| 6 | + |
| 7 | +// Clip-Path |
| 8 | +const Shape = dynamic(import("react-clip-path"), { ssr: false }); |
| 9 | + |
| 10 | +// Switch |
| 11 | +import Switch from "react-switch"; |
| 12 | + |
| 13 | +// Shape Listing Styled-Componentns |
| 14 | +import { |
| 15 | + ShapeCards, |
| 16 | + ShapeCard, |
| 17 | + ShapeName, |
| 18 | + ShapePallete, |
| 19 | + ShapeDetailsItems, |
| 20 | + ShapeCardSwitch, |
| 21 | + ShapeActions, |
| 22 | + ShapeCardHeader, |
| 23 | + CopyIcon, |
| 24 | + DownloadIcon, |
| 25 | + LikeIcon, |
| 26 | + } from './StyledComponents'; |
| 27 | + |
| 28 | +const ShapeList = ({ data }) => { |
| 29 | + |
| 30 | + const [shapes, setShapes] = useState(data); |
| 31 | + |
| 32 | + const handleSwicth = (shapeName) => { |
| 33 | + console.log(shapeName); |
| 34 | + |
| 35 | + let modifiedShapes = shapes.map((shape, index) => { |
| 36 | + if (shape.name === shapeName) { |
| 37 | + return { |
| 38 | + ...shape, |
| 39 | + showAdvanced: !shape.showAdvanced, |
| 40 | + }; |
| 41 | + } |
| 42 | + return shape; |
| 43 | + }); |
| 44 | + console.log(modifiedShapes); |
| 45 | + setShapes(...[modifiedShapes]); |
| 46 | + }; |
| 47 | + |
| 48 | + const getShapeFileName = (name) => { |
| 49 | + return name.split(" ").join("-"); |
| 50 | + }; |
| 51 | + |
| 52 | + async function performCopy(event, formula) { |
| 53 | + event.preventDefault(); |
| 54 | + try { |
| 55 | + await navigator.clipboard.writeText(formula); |
| 56 | + toast.success("Successfully Copied!"); |
| 57 | + console.log("The clip-path formula copied to clipboard"); |
| 58 | + } catch (err) { |
| 59 | + console.error("Failed to copy: ", err); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return ( |
| 64 | + <ShapePallete> |
| 65 | + <ShapeCards> |
| 66 | + {shapes.map((shape, index) => ( |
| 67 | + <React.Fragment key={index}> |
| 68 | + <ShapeCard> |
| 69 | + <ShapeCardHeader> |
| 70 | + <ShapeName>{shape.name}</ShapeName> |
| 71 | + <ShapeActions> |
| 72 | + <span title="Like"> |
| 73 | + <LikeIcon size={24} /> |
| 74 | + </span>{" "} |
| 75 | + <span title="Download"> |
| 76 | + <DownloadIcon |
| 77 | + size={24} |
| 78 | + onClick={(event) => |
| 79 | + saveAsPng( |
| 80 | + event, |
| 81 | + `${getShapeFileName(shape.name)}-id`, |
| 82 | + getShapeFileName(shape.name) |
| 83 | + ) |
| 84 | + } |
| 85 | + /> |
| 86 | + </span> |
| 87 | + </ShapeActions> |
| 88 | + </ShapeCardHeader> |
| 89 | + <Shape |
| 90 | + width="300px" |
| 91 | + height="300px" |
| 92 | + name={shape.name} |
| 93 | + id={`${getShapeFileName(shape.name)}-id`} |
| 94 | + backgroundColor="#eb3d86" |
| 95 | + showShadow={shape.showAdvanced} |
| 96 | + /> |
| 97 | + |
| 98 | + <ShapeCardSwitch> |
| 99 | + <label htmlFor={`${getShapeFileName(shape.name)}-form`}> |
| 100 | + <span>Show Clip-Path Info</span>{" "} |
| 101 | + <Switch |
| 102 | + onChange={() => handleSwicth(shape.name)} |
| 103 | + checked={shape.showAdvanced} |
| 104 | + id={`${getShapeFileName(shape.name)}-form`} |
| 105 | + /> |
| 106 | + </label> |
| 107 | + </ShapeCardSwitch> |
| 108 | + |
| 109 | + {shape.showAdvanced && ( |
| 110 | + <ShapeDetailsItems> |
| 111 | + <span> |
| 112 | + <b>Clip-Path:</b>{" "} |
| 113 | + <code> |
| 114 | + <b>{shape.formula}</b> |
| 115 | + </code> |
| 116 | + </span>{" "} |
| 117 | + <span title="Copy"> |
| 118 | + <CopyIcon |
| 119 | + size={24} |
| 120 | + onClick={(event) => performCopy(event, shape.formula)} |
| 121 | + /> |
| 122 | + </span> |
| 123 | + </ShapeDetailsItems> |
| 124 | + )} |
| 125 | + </ShapeCard> |
| 126 | + </React.Fragment> |
| 127 | + ))} |
| 128 | + </ShapeCards> |
| 129 | + </ShapePallete> |
| 130 | + ); |
| 131 | +}; |
| 132 | + |
| 133 | +export default ShapeList; |
0 commit comments