Skip to content
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

fix(sandpack client): introduce auto reload #877

Merged
merged 4 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion sandpack-react/src/components/CodeEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ export const SandpackCodeEditor = React.forwardRef<
wrapContent={wrapContent}
/>

{showRunButton && status === "idle" ? <RunButton /> : null}
{!sandpack.autoReload || (showRunButton && status === "idle") ? (
<RunButton />
) : null}
</div>
</SandpackStack>
);
Expand Down
4 changes: 2 additions & 2 deletions sandpack-react/src/components/Console/Console.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ export const MaxMessageCount = () => {
<SandpackLayout>
<SandpackCodeEditor />
<SandpackConsole
standalone
maxMessageCount={Number(maxMessageCount)}
standalone
/>
</SandpackLayout>
<div
Expand All @@ -225,9 +225,9 @@ export const MaxMessageCount = () => {
<label style={{ display: "flex", flexDirection: "column", gap: 4 }}>
<span>Max Message Count</span>
<input
onChange={(e) => setMaxMessageCount(+e.target.value)}
type="number"
value={maxMessageCount}
onChange={(e) => setMaxMessageCount(+e.target.value)}
/>
</label>
</div>
Expand Down
2 changes: 2 additions & 0 deletions sandpack-react/src/contexts/sandpackContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export const SandpackProvider: React.FC<SandpackProviderProps> = (props) => {
...fileOperations,
...clientOperations,

autoReload: props.options?.autoReload ?? true,

listen: addListener,
dispatch: dispatchMessage,
}}
Expand Down
19 changes: 18 additions & 1 deletion sandpack-react/src/presets/Sandpack.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export const ExternalResources: React.FC = () => (
/>
);

export const RunnableComponent = (): React.ReactElement => (
export const AutoRun = (): React.ReactElement => (
<Sandpack
files={{
"/App.js": `export default function Kitten() {
Expand All @@ -164,6 +164,23 @@ export const RunnableComponent = (): React.ReactElement => (
/>
);

export const AutoReload = (): React.ReactElement => (
<Sandpack
files={{
"/App.js": `export default function Kitten() {
return (
<img src="https://placekitten.com/200/250" alt="Kitten" />
);
}`,
}}
options={{
autorun: true,
autoReload: false,
}}
template="react"
/>
);

export const InitModeUserVisible: React.FC = () => {
return (
<>
Expand Down
3 changes: 2 additions & 1 deletion sandpack-react/src/presets/Sandpack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const Sandpack: SandpackInternal = ({
recompileMode: options.recompileMode,
recompileDelay: options.recompileDelay,
autorun: options.autorun,
autoReload: options.autoReload,
bundlerURL: options.bundlerURL,
startRoute: options.startRoute,
skipEval: options.skipEval,
Expand Down Expand Up @@ -245,7 +246,7 @@ export const Sandpack: SandpackInternal = ({
style={topRowStyle}
/>
)}

{mode === "tests" && (
<SandpackTests
actionsChildren={actionsChildren}
Expand Down
20 changes: 20 additions & 0 deletions sandpack-react/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,24 @@ export interface SandpackOptions {
*/
initMode?: SandpackInitMode;
initModeObserverOptions?: IntersectionObserverInit;
/**
* Determines whether or not the bundling process should start automatically
* for a component in Sandpack. By default, when the component gets closer
* to the viewport or when the page loads and the component is already in
* the viewport, the bundling process will start automatically. However,
* if this prop is set to false, the bundling process will only start when
* triggered manually by the user.
*/
autorun?: boolean;
/**
* Determines whether or not the component should automatically reload when
* changes are made to the code. When this prop is set to true, any changes
* made to the code will trigger an automatic reload of the component,
* allowing the user to see the changes immediately. However, if this prop
* is set to false, the component will need to be manually reloaded by the
* user to see the changes.
*/
autoReload?: boolean;
recompileMode?: "immediate" | "delayed";
recompileDelay?: number;

Expand Down Expand Up @@ -424,6 +441,7 @@ export interface SandpackInternalOptions<
initMode?: SandpackInitMode;
initModeObserverOptions?: IntersectionObserverInit;
autorun?: boolean;
autoReload?: boolean;
recompileMode?: "immediate" | "delayed";
recompileDelay?: number;
id?: string;
Expand Down Expand Up @@ -525,6 +543,8 @@ export interface SandpackState {
activeFile: string;
startRoute?: string;

autoReload: boolean;

/**
* Returns the current state of the editor, meaning that any
* changes from the original `files` must return a `dirty` value;
Expand Down
13 changes: 9 additions & 4 deletions website/docs/src/pages/getting-started/layout.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,15 @@ export default function App() {
`}
</CodeBlock>

### Autorun
### Autorun & auto reload

By default, the bundling process will start as soon as the component is getting closer to the viewport, or when the page loads, if the component is already in the viewport. But you can allow users to trigger the process manually.
**`autorun`**

When a Sandpack instance is not set on `autorun`, which is the default setting, it will show a `Run` button that initializes the bundling process. This can be handy in situations in which you don't want multiple sandpack instances to run everytime the user loads the page.
Determines whether or not the bundling process should start automatically for a component in Sandpack. By default, when the component gets closer to the viewport or when the page loads and the component is already in the viewport, the bundling process will start automatically. However, if this prop is set to false, the bundling process will only start when triggered manually by the user.

**`autoReload`**

Determines whether or not the component should automatically reload when changes are made to the code. When this prop is set to true, any changes made to the code will trigger an automatic reload of the component, allowing the user to see the changes immediately. However, if this prop is set to false, the component will need to be manually reloaded by the user to see the changes.

<CodeBlock stack>
{`import { Sandpack } from "@codesandbox/sandpack-react";
Expand All @@ -178,7 +182,8 @@ export default function App() {
<Sandpack
template="react"
options={{
autorun: false
autorun: false,
autoReload: false
}}
/>
);
Expand Down