PluginProbe
Code Snippets / 3.10.2
Code Snippets v3.10.2
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / js / components / EditMenu / SnippetForm / fields / SnippetLocationInput.tsx

SnippetLocationInput.tsx in Code Snippets 3.10.2, at js/components/EditMenu/SnippetForm/fields/SnippetLocationInput.tsx

80 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { __ } from '@wordpress/i18n'
2 import React, { useId } from 'react'
3 import Select from 'react-select'
4 import { useSnippetForm } from '../WithSnippetFormContext'
5 import { SNIPPET_TYPE_SCOPES } from '../../../../types/Snippet'
6 import { getSnippetType, isCondition } from '../../../../utils/snippets/snippets'
7 import type { SnippetCodeScope } from '../../../../types/Snippet'
8 import type { SelectOption } from '../../../../types/SelectOption'
9
10 const SCOPE_ICONS: Record<SnippetCodeScope, string> = {
11 'global': 'admin-site',
12 'admin': 'admin-tools',
13 'front-end': 'admin-appearance',
14 'single-use': 'clock',
15 'content': 'shortcode',
16 'head-content': 'editor-code',
17 'body-content': 'editor-code',
18 'footer-content': 'editor-code',
19 'admin-css': 'dashboard',
20 'site-css': 'admin-customizer',
21 'site-head-js': 'media-code',
22 'site-footer-js': 'media-code'
23 }
24
25 const SCOPE_DESCRIPTIONS: Record<SnippetCodeScope, string> = {
26 'global': __('Run everywhere', 'code-snippets'),
27 'admin': __('Only run in administration area', 'code-snippets'),
28 'front-end': __('Only run on site front-end', 'code-snippets'),
29 'single-use': __('Only run once', 'code-snippets'),
30 'content': __('Where inserted in editor', 'code-snippets'),
31 'head-content': __('In site header (<head> section)', 'code-snippets'),
32 'body-content': __('In site content (start of <body>)', 'code-snippets'),
33 'footer-content': __('In site footer (end of <body>)', 'code-snippets'),
34 'site-css': __('Site front-end', 'code-snippets'),
35 'admin-css': __('Administration area', 'code-snippets'),
36 'site-head-js': __('In site header (<head> section)', 'code-snippets'),
37 'site-footer-js': __('In site footer (end of <body>)', 'code-snippets')
38 }
39
40 export const SnippetLocationInput: React.FC = () => {
41 const { snippet, setSnippet, isReadOnly } = useSnippetForm()
42 const locationId = useId()
43
44 const options: SelectOption<SnippetCodeScope>[] = SNIPPET_TYPE_SCOPES[getSnippetType(snippet)]
45 .filter(scope => 'condition' !== scope)
46 .map(scope => ({
47 key: scope,
48 value: scope,
49 label: SCOPE_DESCRIPTIONS[scope]
50 }))
51
52 return isCondition(snippet)
53 ? null
54 : <div className="block-form-field">
55 <label htmlFor={locationId}>
56 {__('Location', 'code-snippets')}
57 </label>
58
59 <Select
60 inputId={locationId}
61 className="code-snippets-select code-snippets-select-location"
62 options={options}
63 isSearchable={false}
64 isDisabled={isReadOnly}
65 styles={{
66 menu: provided => ({ ...provided, zIndex: 9999 }),
67 input: provided => ({ ...provided, ':focus': { boxShadow: 'none' } })
68 }}
69 value={options.find(option => option.value === snippet.scope)}
70 formatOptionLabel={({ label, value }) =>
71 <>
72 <span className={`dashicons dashicons-${SCOPE_ICONS[value]}`} aria-hidden="true"></span>{` ${label}`}
73 </>
74 }
75 onChange={option =>
76 option?.value && setSnippet(previous => ({ ...previous, scope: option.value }))}
77 />
78 </div>
79 }
80