1+ import React , { useState } from "react" ;
2+
3+ // dynamic from Next.js
4+ import dynamic from "next/dynamic" ;
5+
6+ // Bootstrap
7+ import Modal from "react-bootstrap/Modal" ;
8+ import Container from 'react-bootstrap/Container'
9+ import Row from 'react-bootstrap/Row'
10+ import Col from 'react-bootstrap/Col'
11+ import Button from "react-bootstrap/Button" ;
12+ import Form from 'react-bootstrap/Form' ;
13+ import ToggleButtonGroup from 'react-bootstrap/ToggleButtonGroup'
14+ import ToggleButton from 'react-bootstrap/ToggleButton'
15+
16+ // Styled Component
17+ import styled from "styled-components" ;
18+
19+ // Clip-Path
20+ const Shape = dynamic ( import ( "react-clip-path" ) , { ssr : false } ) ;
21+
22+ const ShapeContainer = styled . section `
23+ border: solid 1px var(--color-neutral-30);
24+ padding: 1rem;
25+ ` ;
26+
27+ // Toast
28+ import toast from "react-hot-toast" ;
29+
30+ // icons
31+ import { FiCopy } from 'react-icons/fi' ;
32+
33+ // misc utilities
34+ import { getShapeId } from "../../utils/misc" ;
35+
36+ const CSSDisplay = styled . span `
37+ white-space: pre-line;
38+ ` ;
39+
40+ const CopyIcon = styled ( FiCopy ) `
41+ cursor: pointer;
42+ ` ;
43+
44+ const CopyShapeSource = ( { show, setShow, shape } ) => {
45+ const [ type , setType ] = useState ( 'css' ) ;
46+ const [ selector , setSelector ] = useState ( shape . name ) ;
47+
48+ console . log ( shape ) ;
49+
50+ const handleSelectorChange = evt => {
51+ const value = evt . target . value ;
52+ if ( ! value ) {
53+ setSelector ( shape . name ) ;
54+ } else {
55+ setSelector ( value ) ;
56+ }
57+ }
58+
59+ const getCSSSelector = ( ) => {
60+
61+ return selector . toLowerCase ( ) . split ( ' ' ) . join ( '-' ) ;
62+ }
63+
64+ const getCSS = formula => {
65+ const css = `.${ getCSSSelector ( ) } { \n clip-path: ${ formula } ; \n background-color: #809000; \n width: 300px; \n height: 300px; \n }` ;
66+ console . log ( css ) ;
67+ return css ;
68+ }
69+
70+ const copy = async ( formula , css ) => {
71+ let text = formula ;
72+ if ( css ) {
73+ text = getCSS ( formula ) ;
74+ }
75+ try {
76+ await navigator . clipboard . writeText ( text ) ;
77+ toast . success ( "Successfully Copied!" ) ;
78+ console . log ( "The CSS copied to clipboard" ) ;
79+ } catch ( err ) {
80+ console . error ( "Failed to copy: " , err ) ;
81+ }
82+ }
83+
84+ return (
85+ < >
86+ { true && (
87+ < Modal
88+ size = "lg"
89+ aria-labelledby = "contained-modal-title-vcenter"
90+ show = { show }
91+ onHide = { ( ) => setShow ( false ) }
92+ centered
93+ >
94+ < Modal . Header closeButton >
95+ < Modal . Title > Copy Source for { shape . name } </ Modal . Title >
96+ </ Modal . Header >
97+ < Modal . Body >
98+ < Container fluid >
99+ < Row >
100+ < Col >
101+ < Form >
102+ < div >
103+ < Form . Group >
104+ < Form . Label > Export As</ Form . Label >
105+ < div >
106+ < ToggleButtonGroup type = "radio" name = "options" defaultValue = { 1 } variant = "outline-dark" size = "sm" defaultValue = { type } >
107+ < ToggleButton id = "tbg-radio-1" value = { 'css' } variant = "outline-dark" onClick = { ( ) => setType ( 'css' ) } >
108+ Show CSS
109+ </ ToggleButton >
110+ < ToggleButton id = "tbg-radio-2" value = { 'clip-path' } variant = "outline-dark" onClick = { ( ) => setType ( 'clip-path' ) } >
111+ Show Clip-Path
112+ </ ToggleButton >
113+ </ ToggleButtonGroup >
114+ </ div >
115+ </ Form . Group >
116+ </ div >
117+ {
118+ ( type === 'css' && < Form . Group className = "mb-3" id = "export-name" >
119+ < Form . Label > CSS Selector Name</ Form . Label >
120+ < Form . Control type = "text" name = "name" value = { selector } onChange = { handleSelectorChange } />
121+ </ Form . Group > )
122+ }
123+ {
124+ ( type &&
125+ < div >
126+ { type === 'css' &&
127+ < >
128+ < h3 > CSS Snippet</ h3 >
129+ < CSSDisplay >
130+ < code > { getCSS ( shape . formula ) } </ code >
131+ </ CSSDisplay >
132+ < CopyIcon size = { 20 } onClick = { ( ) => copy ( shape . formula , true ) } />
133+ </ >
134+ }
135+ { type === 'clip-path' &&
136+ < div >
137+ < h3 > Clip-Path Value</ h3 >
138+ < code > { shape . formula } </ code >
139+ < CopyIcon size = { 20 } onClick = { ( ) => copy ( shape . formula , false ) } />
140+ </ div >
141+ }
142+ </ div >
143+ )
144+ }
145+ </ Form >
146+ </ Col >
147+ < Col >
148+ < ShapeContainer >
149+ < Shape
150+ name = { shape . name }
151+ formula = { shape . formula }
152+ width = "300px"
153+ height = "300px"
154+ backgroundColor = { shape . backgroundColor }
155+ id = { getShapeId ( shape . name , true ) }
156+ />
157+ </ ShapeContainer >
158+ </ Col >
159+ </ Row >
160+ </ Container >
161+ </ Modal . Body >
162+
163+ < Modal . Footer >
164+ < Button variant = "outline-info" onClick = { ( ) => setShow ( false ) } >
165+ Cancel
166+ </ Button >
167+ </ Modal . Footer >
168+ </ Modal >
169+ ) }
170+ </ >
171+ )
172+
173+ } ;
174+
175+ export default CopyShapeSource ;
0 commit comments