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

feat: add rtl option #774

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions sandpack-react/src/presets/Sandpack.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,16 @@ Write-Output $example`,
}}
/>
);

export const RtlLayout: React.FC = () => {
return (
<>
<Sandpack
options={{
rtl: true,
}}
template="react"
/>
</>
);
};
22 changes: 19 additions & 3 deletions sandpack-react/src/presets/Sandpack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const Sandpack: SandpackInternal = ({
options.editorWidthPercentage ??= 50;
options.showConsole ??= false;

const rtlLayout = options?.rtl ?? false;
const codeEditorOptions: CodeEditorProps = {
showTabs: options.showTabs,
showLineNumbers: options.showLineNumbers,
Expand Down Expand Up @@ -143,7 +144,7 @@ export const Sandpack: SandpackInternal = ({
const boundaries = Math.min(Math.max(offset, 25), 75);

if (isHorizontal) {
setHorizontalSize(boundaries);
setHorizontalSize(rtlLayout ? 100 - boundaries : boundaries);
} else {
setVerticalSize(boundaries);
}
Expand Down Expand Up @@ -196,7 +197,9 @@ export const Sandpack: SandpackInternal = ({
theme={theme}
{...props}
>
<SandpackLayout>
<SandpackLayout
className={rtlLayout ? classNames(rtlLayoutClassName) : ""}
>
<SandpackCodeEditor
{...codeEditorOptions}
style={{
Expand All @@ -218,7 +221,11 @@ export const Sandpack: SandpackInternal = ({
onMouseDown={(event): void => {
dragEventTargetRef.current = event.target;
}}
style={{ left: `calc(${horizontalSize}% - 5px)` }}
style={{
left: `calc(${
rtlLayout ? 100 - horizontalSize : horizontalSize
}% - 5px)`,
}}
/>
)}

Expand Down Expand Up @@ -337,3 +344,12 @@ const consoleWrapper = css({
width: "100%",
overflow: "hidden",
});

const rtlLayoutClassName = css({
flexDirection: "row-reverse",

"@media screen and (max-width: 768px)": {
flexFlow: "wrap-reverse !important",
flexDirection: "initial",
},
});
10 changes: 10 additions & 0 deletions sandpack-react/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ export interface SandpackOptions {
editorHeight?: React.CSSProperties["height"];
classes?: Record<string, string>;

/**
* right to left layout
* @default false
*/
rtl?: boolean;
showNavigator?: boolean;
showLineNumbers?: boolean;
showInlineErrors?: boolean;
Expand Down Expand Up @@ -438,6 +443,11 @@ interface SandpackInternalProps<
editorWidthPercentage?: number;
editorHeight?: React.CSSProperties["height"];

/**
* right to left layout
* @default false
*/
rtl?: boolean;
showNavigator?: boolean;
showLineNumbers?: boolean;
showInlineErrors?: boolean;
Expand Down
21 changes: 20 additions & 1 deletion website/docs/src/pages/getting-started/layout.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,23 @@ The `<Sandpack />` preset component has resizable columns and rows by default, a
<Sandpack options={{ resizablePanels: false }} />
```

Other components (`SandpackProvider` for example) do not have this functionality and it must be implemented by the user.
Other components (`SandpackProvider` for example) do not have this functionality and it must be implemented by the user.

### Right to left layout

<CodeBlock stack>
{`import { Sandpack } from "@codesandbox/sandpack-react";

export default function App() {
return (
<Sandpack
options={{
rtl: true, // default false
}}
/>
);
}
`}
</CodeBlock>

Other components (`SandpackProvider` for example) do not have this option and it must be implemented by the user.