-
Notifications
You must be signed in to change notification settings - Fork 7.8k
[Beta] Sandpack bundler improvements #5164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gaearon
merged 19 commits into
reactjs:main
from
danilowoz:sandpack-bundler-improvements
Oct 25, 2022
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
d6f236f
cache babel assets
danilowoz 0b2205e
Update SandpackRoot.tsx
danilowoz 46d2c22
Update NavigationBar.tsx
danilowoz 0548b03
Update SandpackRoot.tsx
danilowoz db4db1b
Update 7 files
danilowoz ba32742
Update 6 files
danilowoz b5f28a7
Update 6 files
danilowoz bcad25b
Update LoadingOverlay.tsx
danilowoz 5020a4d
Update Preview.tsx
danilowoz 8dea21f
Update LoadingOverlay.tsx
danilowoz 878c210
Update 4 files
danilowoz 5a7731f
Update beta/src/components/MDX/Sandpack/LoadingOverlay.tsx
danilowoz fc65c3a
Update sandpack.css
danilowoz 08cb840
Update Preview.tsx and SandpackRoot.tsx
danilowoz 92688e8
Update 3 files
danilowoz 5f431d1
Update ErrorMessage.tsx and Preview.tsx
danilowoz f2b84c6
Update Preview.tsx
danilowoz 7c85423
Update SandpackRoot.tsx
danilowoz c6ffbac
Update Preview.tsx
danilowoz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import {useState} from 'react'; | ||
|
||
import { | ||
LoadingOverlayState, | ||
OpenInCodeSandboxButton, | ||
useSandpack, | ||
} from '@codesandbox/sandpack-react'; | ||
import {useEffect} from 'react'; | ||
|
||
const FADE_ANIMATION_DURATION = 200; | ||
|
||
export const LoadingOverlay = ({ | ||
clientId, | ||
dependenciesLoading, | ||
forceLoading, | ||
}: { | ||
clientId: string; | ||
dependenciesLoading: boolean; | ||
forceLoading: boolean; | ||
} & React.HTMLAttributes<HTMLDivElement>): JSX.Element | null => { | ||
const loadingOverlayState = useLoadingOverlayState( | ||
clientId, | ||
dependenciesLoading, | ||
forceLoading | ||
); | ||
|
||
if (loadingOverlayState === 'HIDDEN') { | ||
return null; | ||
} | ||
|
||
if (loadingOverlayState === 'TIMEOUT') { | ||
return ( | ||
<div className="sp-overlay sp-error"> | ||
<div className="sp-error-message"> | ||
Unable to establish connection with the sandpack bundler. Make sure | ||
you are online or try again later. If the problem persists, please | ||
report it via{' '} | ||
<a | ||
className="sp-error-message" | ||
href="mailto:hello@codesandbox.io?subject=Sandpack Timeout Error"> | ||
</a>{' '} | ||
or submit an issue on{' '} | ||
<a | ||
className="sp-error-message" | ||
href="https://github.com/codesandbox/sandpack/issues" | ||
rel="noreferrer noopener" | ||
target="_blank"> | ||
GitHub. | ||
</a> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
const stillLoading = | ||
loadingOverlayState === 'LOADING' || loadingOverlayState === 'PRE_FADING'; | ||
|
||
return ( | ||
<div | ||
className="sp-overlay sp-loading" | ||
style={{ | ||
opacity: stillLoading ? 1 : 0, | ||
transition: `opacity ${FADE_ANIMATION_DURATION}ms ease-out`, | ||
}}> | ||
<div className="sp-cube-wrapper" title="Open in CodeSandbox"> | ||
<OpenInCodeSandboxButton /> | ||
<div className="sp-cube"> | ||
<div className="sp-sides"> | ||
<div className="top" /> | ||
<div className="right" /> | ||
<div className="bottom" /> | ||
<div className="left" /> | ||
<div className="front" /> | ||
<div className="back" /> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
const useLoadingOverlayState = ( | ||
clientId: string, | ||
dependenciesLoading: boolean, | ||
forceLoading: boolean | ||
): LoadingOverlayState => { | ||
const {sandpack, listen} = useSandpack(); | ||
const [state, setState] = useState<LoadingOverlayState>('HIDDEN'); | ||
|
||
if (state !== 'LOADING' && forceLoading) { | ||
setState('LOADING'); | ||
} | ||
|
||
/** | ||
* Sandpack listener | ||
*/ | ||
const sandpackIdle = sandpack.status === 'idle'; | ||
useEffect(() => { | ||
const unsubscribe = listen((message) => { | ||
if (message.type === 'done') { | ||
setState((prev) => { | ||
return prev === 'LOADING' ? 'PRE_FADING' : 'HIDDEN'; | ||
}); | ||
} | ||
}, clientId); | ||
|
||
return () => { | ||
unsubscribe(); | ||
}; | ||
}, [listen, clientId, sandpackIdle]); | ||
|
||
/** | ||
* Fading transient state | ||
*/ | ||
useEffect(() => { | ||
let fadeTimeout: ReturnType<typeof setTimeout>; | ||
|
||
if (state === 'PRE_FADING' && !dependenciesLoading) { | ||
setState('FADING'); | ||
} else if (state === 'FADING') { | ||
fadeTimeout = setTimeout( | ||
() => setState('HIDDEN'), | ||
FADE_ANIMATION_DURATION | ||
); | ||
} | ||
|
||
return () => { | ||
clearTimeout(fadeTimeout); | ||
}; | ||
}, [state, dependenciesLoading]); | ||
|
||
if (sandpack.status === 'timeout') { | ||
return 'TIMEOUT'; | ||
} | ||
|
||
if (sandpack.status !== 'running') { | ||
return 'HIDDEN'; | ||
} | ||
|
||
return state; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.