| 1 |
import { |
| 2 |
BaseControl, |
| 3 |
CheckboxControl, |
| 4 |
TextControl, |
| 5 |
} from '@wordpress/components'; |
| 6 |
import { __ } from '@wordpress/i18n'; |
| 7 |
import classNames from 'classnames'; |
| 8 |
import { useThemeStore } from '../../../../state/theme'; |
| 9 |
import { AttributesPropsAndSetter } from '../../../../types'; |
| 10 |
import { copyButtonTypes } from '../copy/CopyButton'; |
| 11 |
|
| 12 |
export const CopyBtnSettings = ({ |
| 13 |
attributes, |
| 14 |
setAttributes, |
| 15 |
}: AttributesPropsAndSetter) => { |
| 16 |
const { updateThemeHistory } = useThemeStore(); |
| 17 |
return ( |
| 18 |
<BaseControl id="code-block-pro-show-copy-button"> |
| 19 |
<CheckboxControl |
| 20 |
data-cy="copy-button" |
| 21 |
label={__('Copy Button', 'code-block-pro')} |
| 22 |
help={__('Adds a button to copy the code', 'code-block-pro')} |
| 23 |
checked={attributes.copyButton} |
| 24 |
onChange={(copyButton) => { |
| 25 |
setAttributes({ copyButton }); |
| 26 |
updateThemeHistory({ copyButton }); |
| 27 |
}} |
| 28 |
/> |
| 29 |
{attributes.copyButton && ( |
| 30 |
<div className="flex gap-2"> |
| 31 |
{Object.entries(copyButtonTypes).map(([slug, data]) => { |
| 32 |
const { Component } = data; |
| 33 |
const { |
| 34 |
copyButtonType, |
| 35 |
bgColor: backgroundColor, |
| 36 |
textColor: color, |
| 37 |
} = attributes; |
| 38 |
const current = copyButtonType === slug; |
| 39 |
return ( |
| 40 |
<div key={slug} style={{ backgroundColor }}> |
| 41 |
<button |
| 42 |
id={`code-block-pro-copy-button-${slug}`} |
| 43 |
type="button" |
| 44 |
onClick={() => { |
| 45 |
setAttributes({ copyButtonType: slug }); |
| 46 |
updateThemeHistory({ |
| 47 |
copyButtonType: slug, |
| 48 |
}); |
| 49 |
}} |
| 50 |
style={{ color }} |
| 51 |
aria-current={current} |
| 52 |
className={classNames( |
| 53 |
'sidebar-copy-button relative border-0 flex items-start w-full text-left outline-none cursor-pointer no-underline ring-offset-2 ring-offset-white focus:shadow-none ring-wp-theme-500 focus:ring-wp overflow-x-auto top-0 right-0 left-auto bg-transparent border-none transition-opacity duration-200 ease-in-out leading-none z-10 p-1 opacity-90', |
| 54 |
{ |
| 55 |
'ring-wp': current, |
| 56 |
}, |
| 57 |
)}> |
| 58 |
<span className="sr-only"> |
| 59 |
{data.label} |
| 60 |
</span> |
| 61 |
<span className="pointer-events-none w-full"> |
| 62 |
<Component color={color} /> |
| 63 |
</span> |
| 64 |
</button> |
| 65 |
</div> |
| 66 |
); |
| 67 |
})} |
| 68 |
</div> |
| 69 |
)} |
| 70 |
{attributes.copyButton && ( |
| 71 |
<div style={{ marginTop: '1rem' }}> |
| 72 |
<TextControl |
| 73 |
id="code-block-pro-copy-button-text" |
| 74 |
autoComplete="off" |
| 75 |
label={__('Button label', 'code-block-pro')} |
| 76 |
placeholder={__('Copy', 'code-block-pro')} |
| 77 |
onChange={(copyButtonString) => { |
| 78 |
setAttributes({ copyButtonString }); |
| 79 |
}} |
| 80 |
value={attributes.copyButtonString ?? ''} |
| 81 |
/> |
| 82 |
{attributes.copyButtonType === 'textSimple' && ( |
| 83 |
<TextControl |
| 84 |
id="code-block-pro-copy-button-text-copied" |
| 85 |
autoComplete="off" |
| 86 |
label={__('Copied Text', 'code-block-pro')} |
| 87 |
placeholder={__('Copied!', 'code-block-pro')} |
| 88 |
onChange={(copyButtonStringCopied) => { |
| 89 |
setAttributes({ copyButtonStringCopied }); |
| 90 |
}} |
| 91 |
value={attributes.copyButtonStringCopied ?? ''} |
| 92 |
help={__( |
| 93 |
'Text briefly shown after the code is copied', |
| 94 |
)} |
| 95 |
/> |
| 96 |
)} |
| 97 |
<CheckboxControl |
| 98 |
label={__('Use Textarea', 'code-block-pro')} |
| 99 |
help={__( |
| 100 |
'Use a <textarea> to to store the code, otherwise a `data-code` attribute', |
| 101 |
'code-block-pro', |
| 102 |
)} |
| 103 |
checked={attributes.copyButtonUseTextarea} |
| 104 |
onChange={(copyButtonUseTextarea) => { |
| 105 |
setAttributes({ copyButtonUseTextarea }); |
| 106 |
updateThemeHistory({ copyButtonUseTextarea }); |
| 107 |
}} |
| 108 |
/> |
| 109 |
</div> |
| 110 |
)} |
| 111 |
</BaseControl> |
| 112 |
); |
| 113 |
}; |
| 114 |
|