2
2
* Copyright (c) Facebook, Inc. and its affiliates.
3
3
*/
4
4
5
- import {
5
+ import React , {
6
6
useRef ,
7
7
useInsertionEffect ,
8
8
useCallback ,
@@ -20,7 +20,10 @@ import {
20
20
import { OpenInCodeSandboxButton } from './OpenInCodeSandboxButton' ;
21
21
import { ResetButton } from './ResetButton' ;
22
22
import { DownloadButton } from './DownloadButton' ;
23
- import { SnippetTargetLanguageContext } from './SnippetLanguage' ;
23
+ import {
24
+ SnippetTargetLanguage ,
25
+ SnippetTargetLanguageContext ,
26
+ } from './SnippetLanguage' ;
24
27
import { IconChevron } from '../../Icon/IconChevron' ;
25
28
import { Listbox } from '@headlessui/react' ;
26
29
import classNames from 'classnames' ;
@@ -42,44 +45,70 @@ const getFileName = (filePath: string): string => {
42
45
return filePath . slice ( lastIndexOfSlash + 1 ) ;
43
46
} ;
44
47
45
- function SnippetTargetLanguageButton ( props : any ) {
46
- const { children, snippetTargetLanguage} = props ;
47
- const {
48
- setSnippetTargetLanguage,
49
- snippetTargetLanguage : currentSnippetTargetLanguage ,
50
- } = useContext ( SnippetTargetLanguageContext ) ;
51
-
52
- const isCurrent = currentSnippetTargetLanguage === snippetTargetLanguage ;
48
+ function SnippetTargetLanguageButton ( {
49
+ children,
50
+ active,
51
+ disabled,
52
+ snippetTargetLanguage,
53
+ } : {
54
+ children : React . ReactNode ;
55
+ active : boolean ;
56
+ disabled ?: boolean | undefined ;
57
+ snippetTargetLanguage : SnippetTargetLanguage ;
58
+ } ) {
59
+ const { setSnippetTargetLanguage} = useContext ( SnippetTargetLanguageContext ) ;
53
60
54
61
return (
55
62
< button
56
- aria-pressed = { isCurrent ? true : undefined }
63
+ aria-pressed = { active }
64
+ aria-disabled = { disabled }
57
65
className = { classNames (
58
66
'text-sm text-primary dark:text-primary-dark inline-flex mx-1 border-black' ,
59
- isCurrent && 'border-b-4'
67
+ active && 'border-b-4' ,
68
+ disabled && 'cursor-not-allowed'
60
69
) }
61
- onClick = { ( ) => setSnippetTargetLanguage ( snippetTargetLanguage ) }
70
+ onClick = { ( ) => {
71
+ if ( ! disabled ) {
72
+ setSnippetTargetLanguage ( snippetTargetLanguage ) ;
73
+ }
74
+ } }
75
+ title = { disabled ? 'Language not available at the moment.' : undefined }
62
76
type = "button" >
63
77
{ children }
64
78
</ button >
65
79
) ;
66
80
}
67
81
68
- function SnippetTargetLanguageToggle ( ) {
82
+ function SnippetTargetLanguageToggle ( { hasTSVersion} : { hasTSVersion : boolean } ) {
83
+ const { snippetTargetLanguage} = useContext ( SnippetTargetLanguageContext ) ;
84
+
69
85
return (
70
86
< div role = "group" aria-label = "Snippet target language" >
71
- < SnippetTargetLanguageButton key = "js" snippetTargetLanguage = "js" >
87
+ < SnippetTargetLanguageButton
88
+ key = "js"
89
+ active = { ! hasTSVersion || snippetTargetLanguage === 'js' }
90
+ snippetTargetLanguage = "js" >
72
91
JS
73
92
</ SnippetTargetLanguageButton >
74
93
75
- < SnippetTargetLanguageButton key = "ts" snippetTargetLanguage = "ts" >
94
+ < SnippetTargetLanguageButton
95
+ key = "ts"
96
+ active = { hasTSVersion && snippetTargetLanguage === 'ts' }
97
+ disabled = { ! hasTSVersion }
98
+ snippetTargetLanguage = "ts" >
76
99
TS
77
100
</ SnippetTargetLanguageButton >
78
101
</ div >
79
102
) ;
80
103
}
81
104
82
- export function NavigationBar ( { providedFiles} : { providedFiles : Array < string > } ) {
105
+ export function NavigationBar ( {
106
+ hasTSVersion,
107
+ providedFiles,
108
+ } : {
109
+ hasTSVersion : boolean ;
110
+ providedFiles : Array < string > ;
111
+ } ) {
83
112
const { sandpack} = useSandpack ( ) ;
84
113
const containerRef = useRef < HTMLDivElement | null > ( null ) ;
85
114
const tabsRef = useRef < HTMLDivElement | null > ( null ) ;
@@ -219,7 +248,7 @@ export function NavigationBar({providedFiles}: {providedFiles: Array<string>}) {
219
248
< div
220
249
className = "px-3 flex items-center justify-end text-right"
221
250
translate = "yes" >
222
- < SnippetTargetLanguageToggle />
251
+ < SnippetTargetLanguageToggle hasTSVersion = { hasTSVersion } />
223
252
< DownloadButton providedFiles = { providedFiles } />
224
253
< ResetButton onReset = { handleReset } />
225
254
< OpenInCodeSandboxButton />
0 commit comments