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: Allow CSS units in viewerHeight option #140

Merged
merged 1 commit into from
Jun 14, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/Components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type { FileContent, FileContentJson } from "./filecontent";
import { FCorFCJSONtoFC } from "./filecontent";
import { fetchGist, gistApiResponseToFileContents } from "./gist";
import { editorUrlPrefix, fileContentsToUrlString } from "./share";
import { asCssLengthUnit } from "./utils";

// Load Editor component dynamically and lazily because it's large and not
// needed for all configurations.
Expand Down Expand Up @@ -82,8 +83,8 @@ type AppOptions = {
// to the editor-viewer app mode.
layout?: "horizontal" | "vertical";

// Height of viewer in pixels.
viewerHeight?: number;
// Height of viewer in pixels (number) or as a CSS string (e.g. 3rem).
viewerHeight?: number | string;

// If the ExampleSelector component is present, which example, if any, should
// start selected?
Expand Down Expand Up @@ -477,7 +478,7 @@ export function App({
);
} else if (appMode === "editor-viewer") {
const layout = appOptions.layout ?? "horizontal";
const viewerHeight = Number(appOptions.viewerHeight ?? 200);
const viewerHeight = asCssLengthUnit(appOptions.viewerHeight) || "200px";

const gridDef =
layout === "horizontal"
Expand All @@ -488,7 +489,7 @@ export function App({
}
: {
areas: [["editor"], ["viewer"]],
rowSizes: ["auto", `${viewerHeight}px`],
rowSizes: ["auto", viewerHeight],
colSizes: ["1fr"],
};

Expand Down Expand Up @@ -526,9 +527,7 @@ export function App({
<div
className="shinylive-container viewer"
style={{
height: appOptions.viewerHeight
? `${appOptions.viewerHeight}px`
: undefined,
height: asCssLengthUnit(appOptions.viewerHeight),
}}
>
<Viewer
Expand Down
15 changes: 15 additions & 0 deletions src/Components/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function asCssLengthUnit(value?: number | string): string | undefined {
if (value === undefined) {
return undefined;
}

if (typeof value === "string") {
return value;
}

if (typeof value !== "number") {
return undefined;
}
Comment on lines +10 to +12
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would it get here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it'd have to be a value that makes it past the type checker. Happy to take this out if its unnecessary.


return `${value}px`;
}