Merge pull request #622 from quantified-uncertainty/May-tailwind-refactor

This commit is contained in:
Ozzie Gooen 2022-06-02 05:17:04 -07:00 committed by GitHub
commit 7f3cacae59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 549 additions and 209 deletions

View File

@ -3,6 +3,8 @@
"version": "0.2.20", "version": "0.2.20",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@headlessui/react": "^1.6.4",
"@heroicons/react": "^1.0.6",
"@hookform/resolvers": "^2.8.10", "@hookform/resolvers": "^2.8.10",
"@quri/squiggle-lang": "^0.2.8", "@quri/squiggle-lang": "^0.2.8",
"@react-hook/size": "^2.1.2", "@react-hook/size": "^2.1.2",
@ -28,6 +30,7 @@
"@storybook/node-logger": "^6.5.6", "@storybook/node-logger": "^6.5.6",
"@storybook/preset-create-react-app": "^4.1.1", "@storybook/preset-create-react-app": "^4.1.1",
"@storybook/react": "^6.5.6", "@storybook/react": "^6.5.6",
"@tailwindcss/forms": "^0.5.2",
"@testing-library/jest-dom": "^5.16.4", "@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.3.0", "@testing-library/react": "^13.3.0",
"@testing-library/user-event": "^14.2.0", "@testing-library/user-event": "^14.2.0",

View File

@ -0,0 +1,83 @@
import * as React from "react";
import {
XCircleIcon,
InformationCircleIcon,
CheckCircleIcon,
} from "@heroicons/react/solid";
export const Alert: React.FC<{
heading: string;
backgroundColor: string;
headingColor: string;
bodyColor: string;
icon: React.ReactNode;
children: React.ReactNode;
}> = ({
heading = "Error",
backgroundColor,
headingColor,
bodyColor,
icon,
children,
}) => {
return (
<div className={`rounded-md p-4 ${backgroundColor}`}>
<div className="flex">
<div className="flex-shrink-0">{icon}</div>
<div className="ml-3">
<h3 className={`text-sm font-medium ${headingColor}`}>{heading}</h3>
<div className={`mt-2 text-sm ${bodyColor}`}>{children}</div>
</div>
</div>
</div>
);
};
export const ErrorAlert: React.FC<{
heading: string;
children: React.ReactNode;
}> = ({ heading = "Error", children }) => (
<Alert
heading={heading}
children={children}
backgroundColor="bg-red-100"
headingColor="text-red-800"
bodyColor="text-red-700"
icon={<XCircleIcon className="h-5 w-5 text-red-400" aria-hidden="true" />}
/>
);
export const MessageAlert: React.FC<{
heading: string;
children: React.ReactNode;
}> = ({ heading = "Error", children }) => (
<Alert
heading={heading}
children={children}
backgroundColor="bg-slate-100"
headingColor="text-slate-700"
bodyColor="text-slate-700"
icon={
<InformationCircleIcon
className="h-5 w-5 text-slate-400"
aria-hidden="true"
/>
}
/>
);
export const SuccessAlert: React.FC<{
heading: string;
children: React.ReactNode;
}> = ({ heading = "Error", children }) => (
<Alert
heading={heading}
children={children}
backgroundColor="bg-green-50"
headingColor="text-green-800"
bodyColor="text-green-700"
icon={
<CheckCircleIcon className="h-5 w-5 text-green-400" aria-hidden="true" />
}
/>
);

View File

@ -29,6 +29,7 @@ export let CodeEditor: FC<CodeEditorProps> = ({
mode="golang" mode="golang"
theme="github" theme="github"
width={"100%"} width={"100%"}
fontSize={14}
height={String(height) + "px"} height={String(height) + "px"}
minLines={oneLine ? lineCount : undefined} minLines={oneLine ? lineCount : undefined}
maxLines={oneLine ? lineCount : undefined} maxLines={oneLine ? lineCount : undefined}

View File

@ -8,7 +8,7 @@ import {
} from "@quri/squiggle-lang"; } from "@quri/squiggle-lang";
import { Vega, VisualizationSpec } from "react-vega"; import { Vega, VisualizationSpec } from "react-vega";
import * as chartSpecification from "../vega-specs/spec-distributions.json"; import * as chartSpecification from "../vega-specs/spec-distributions.json";
import { ErrorBox } from "./ErrorBox"; import { ErrorAlert } from "./Alert";
import { useSize } from "react-use"; import { useSize } from "react-use";
import { import {
linearXScale, linearXScale,
@ -45,6 +45,12 @@ export const DistributionChart: React.FC<DistributionChartProps> = ({
shape.value.discrete.some((x) => x.x <= 0); shape.value.discrete.some((x) => x.x <= 0);
let spec = buildVegaSpec(isLogX, isExpY); let spec = buildVegaSpec(isLogX, isExpY);
let widthProp = width ? width : size.width; let widthProp = width ? width : size.width;
if (widthProp < 20) {
console.warn(
`Width of Distribution is set to ${widthProp}, which is too small`
);
widthProp = 20;
}
// Check whether we should disable the checkbox // Check whether we should disable the checkbox
var logCheckbox = ( var logCheckbox = (
@ -86,9 +92,9 @@ export const DistributionChart: React.FC<DistributionChartProps> = ({
); );
} else { } else {
var result = ( var result = (
<ErrorBox heading="Distribution Error"> <ErrorAlert heading="Distribution Error">
{distributionErrorToString(shape.value)} {distributionErrorToString(shape.value)}
</ErrorBox> </ErrorAlert>
); );
} }
@ -157,18 +163,24 @@ const SummaryTable: React.FC<SummaryTableProps> = ({
return <NumberShower number={x.value} />; return <NumberShower number={x.value} />;
} else { } else {
return ( return (
<ErrorBox heading="Distribution Error"> <ErrorAlert heading="Distribution Error">
{distributionErrorToString(x.value)} {distributionErrorToString(x.value)}
</ErrorBox> </ErrorAlert>
); );
} }
}; };
let TableHeadCell: React.FC<{ children: React.ReactNode }> = ({ let TableHeadCell: React.FC<{ children: React.ReactNode }> = ({
children, children,
}) => <th className="border border-slate-400 bg-slate-50 p-4">{children}</th>; }) => (
<th className="border border-slate-200 bg-slate-50 py-1 px-2 text-slate-500 font-semibold">
{children}
</th>
);
let Cell: React.FC<{ children: React.ReactNode }> = ({ children }) => ( let Cell: React.FC<{ children: React.ReactNode }> = ({ children }) => (
<td className="border border-slate-400 p-4">{children}</td> <td className="border border-slate-200 py-1 px-2 text-slate-900 ">
{children}
</td>
); );
return ( return (

View File

@ -1,12 +0,0 @@
import * as React from "react";
export const ErrorBox: React.FC<{
heading: string;
children: React.ReactNode;
}> = ({ heading = "Error", children }) => {
return (
<div className="border border-red-200 bg-gray-50 p-4">
<h3>{heading}</h3>
{children}
</div>
);
};

View File

@ -2,7 +2,7 @@ import * as React from "react";
import { lambdaValue, environment, runForeign } from "@quri/squiggle-lang"; import { lambdaValue, environment, runForeign } from "@quri/squiggle-lang";
import { FunctionChart1Dist } from "./FunctionChart1Dist"; import { FunctionChart1Dist } from "./FunctionChart1Dist";
import { FunctionChart1Number } from "./FunctionChart1Number"; import { FunctionChart1Number } from "./FunctionChart1Number";
import { ErrorBox } from "./ErrorBox"; import { ErrorAlert, MessageAlert } from "./Alert";
export type FunctionChartSettings = { export type FunctionChartSettings = {
start: number; start: number;
@ -23,9 +23,16 @@ export const FunctionChart: React.FC<FunctionChartProps> = ({
environment, environment,
height, height,
}: FunctionChartProps) => { }: FunctionChartProps) => {
let result1 = runForeign(fn, [chartSettings.start], environment); if (fn.parameters.length > 1) {
let result2 = runForeign(fn, [chartSettings.stop], environment); return (
let getValidResult = () => { <MessageAlert heading="Function Display Not Supported">
Only functions with one parameter are displayed.
</MessageAlert>
);
}
const result1 = runForeign(fn, [chartSettings.start], environment);
const result2 = runForeign(fn, [chartSettings.stop], environment);
const getValidResult = () => {
if (result1.tag === "Ok") { if (result1.tag === "Ok") {
return result1; return result1;
} else if (result2.tag === "Ok") { } else if (result2.tag === "Ok") {
@ -34,41 +41,39 @@ export const FunctionChart: React.FC<FunctionChartProps> = ({
return result1; return result1;
} }
}; };
let validResult = getValidResult(); const validResult = getValidResult();
let resultType = validResult.tag === "Ok" ? validResult.value.tag : "Error"; const resultType =
validResult.tag === "Ok" ? validResult.value.tag : ("Error" as const);
let component = () => { switch (resultType) {
switch (resultType) { case "distribution":
case "distribution": return (
return ( <FunctionChart1Dist
<FunctionChart1Dist fn={fn}
fn={fn} chartSettings={chartSettings}
chartSettings={chartSettings} environment={environment}
environment={environment} height={height}
height={height} />
/> );
); case "number":
case "number": return (
return ( <FunctionChart1Number
<FunctionChart1Number fn={fn}
fn={fn} chartSettings={chartSettings}
chartSettings={chartSettings} environment={environment}
environment={environment} height={height}
height={height} />
/> );
); case "Error":
case "Error": return (
return ( <ErrorAlert heading="Error">The function failed to be run</ErrorAlert>
<ErrorBox heading="Error">The function failed to be run</ErrorBox> );
); default:
default: return (
return ( <MessageAlert heading="Function Display Not Supported">
<ErrorBox heading="No Viewer"> There is no function visualization for this type of output:{" "}
There is no function visualization for this type of function <span className="font-bold">{resultType}</span>
</ErrorBox> </MessageAlert>
); );
} }
};
return component();
}; };

View File

@ -15,7 +15,7 @@ import { createClassFromSpec } from "react-vega";
import * as percentilesSpec from "../vega-specs/spec-percentiles.json"; import * as percentilesSpec from "../vega-specs/spec-percentiles.json";
import { DistributionChart } from "./DistributionChart"; import { DistributionChart } from "./DistributionChart";
import { NumberShower } from "./NumberShower"; import { NumberShower } from "./NumberShower";
import { ErrorBox } from "./ErrorBox"; import { ErrorAlert } from "./Alert";
let SquigglePercentilesChart = createClassFromSpec({ let SquigglePercentilesChart = createClassFromSpec({
spec: percentilesSpec as Spec, spec: percentilesSpec as Spec,
@ -197,7 +197,7 @@ export const FunctionChart1Dist: React.FC<FunctionChart1DistProps> = ({
{showChart} {showChart}
{_.entries(getPercentilesMemoized.errors).map( {_.entries(getPercentilesMemoized.errors).map(
([errorName, errorPoints]) => ( ([errorName, errorPoints]) => (
<ErrorBox key={errorName} heading={errorName}> <ErrorAlert key={errorName} heading={errorName}>
Values:{" "} Values:{" "}
{errorPoints {errorPoints
.map((r, i) => <NumberShower key={i} number={r.x} />) .map((r, i) => <NumberShower key={i} number={r.x} />)
@ -206,7 +206,7 @@ export const FunctionChart1Dist: React.FC<FunctionChart1DistProps> = ({
{a}, {b} {a}, {b}
</> </>
))} ))}
</ErrorBox> </ErrorAlert>
) )
)} )}
</> </>

View File

@ -10,7 +10,7 @@ import {
} from "@quri/squiggle-lang"; } from "@quri/squiggle-lang";
import { createClassFromSpec } from "react-vega"; import { createClassFromSpec } from "react-vega";
import * as lineChartSpec from "../vega-specs/spec-line-chart.json"; import * as lineChartSpec from "../vega-specs/spec-line-chart.json";
import { ErrorBox } from "./ErrorBox"; import { ErrorAlert } from "./Alert";
let SquiggleLineChart = createClassFromSpec({ let SquiggleLineChart = createClassFromSpec({
spec: lineChartSpec as Spec, spec: lineChartSpec as Spec,
@ -110,9 +110,9 @@ export const FunctionChart1Number: React.FC<FunctionChart1NumberProps> = ({
actions={false} actions={false}
/> />
{getFunctionImageMemoized.errors.map(({ x, value }) => ( {getFunctionImageMemoized.errors.map(({ x, value }) => (
<ErrorBox key={x} heading={value}> <ErrorAlert key={x} heading={value}>
Error at point ${x} Error at point ${x}
</ErrorBox> </ErrorAlert>
))} ))}
</> </>
); );

View File

@ -14,7 +14,7 @@ import {
} from "@quri/squiggle-lang"; } from "@quri/squiggle-lang";
import { NumberShower } from "./NumberShower"; import { NumberShower } from "./NumberShower";
import { DistributionChart } from "./DistributionChart"; import { DistributionChart } from "./DistributionChart";
import { ErrorBox } from "./ErrorBox"; import { ErrorAlert } from "./Alert";
import { FunctionChart, FunctionChartSettings } from "./FunctionChart"; import { FunctionChart, FunctionChartSettings } from "./FunctionChart";
function getRange<a>(x: declaration<a>) { function getRange<a>(x: declaration<a>) {
@ -54,7 +54,7 @@ export const VariableBox: React.FC<VariableBoxProps> = ({
return ( return (
<div className="bg-white border border-grey-200 m-2"> <div className="bg-white border border-grey-200 m-2">
<div className="border-b border-grey-200 p-3"> <div className="border-b border-grey-200 p-3">
<h3>{heading}</h3> <h3 className="font-mono">{heading}</h3>
</div> </div>
<div className="p-3">{children}</div> <div className="p-3">{children}</div>
</div> </div>
@ -95,7 +95,9 @@ const SquiggleItem: React.FC<SquiggleItemProps> = ({
case "number": case "number":
return ( return (
<VariableBox heading="Number" showTypes={showTypes}> <VariableBox heading="Number" showTypes={showTypes}>
<NumberShower precision={3} number={expression.value} /> <div className="font-semibold text-slate-600">
<NumberShower precision={3} number={expression.value} />
</div>
</VariableBox> </VariableBox>
); );
case "distribution": { case "distribution": {
@ -124,10 +126,13 @@ const SquiggleItem: React.FC<SquiggleItemProps> = ({
} }
case "string": case "string":
return ( return (
<VariableBox <VariableBox heading="String" showTypes={showTypes}>
heading="String" <span className="text-slate-400">"</span>
showTypes={showTypes} <span className="text-slate-600 font-semibold">
>{`"${expression.value}"`}</VariableBox> {expression.value}
</span>
<span className="text-slate-400">"</span>
</VariableBox>
); );
case "boolean": case "boolean":
return ( return (
@ -138,7 +143,8 @@ const SquiggleItem: React.FC<SquiggleItemProps> = ({
case "symbol": case "symbol":
return ( return (
<VariableBox heading="Symbol" showTypes={showTypes}> <VariableBox heading="Symbol" showTypes={showTypes}>
{expression.value} <span className="text-slate-500 mr-2">Undefined Symbol:</span>
<span className="text-slate-600">{expression.value}</span>
</VariableBox> </VariableBox>
); );
case "call": case "call":
@ -151,17 +157,24 @@ const SquiggleItem: React.FC<SquiggleItemProps> = ({
return ( return (
<VariableBox heading="Array" showTypes={showTypes}> <VariableBox heading="Array" showTypes={showTypes}>
{expression.value.map((r, i) => ( {expression.value.map((r, i) => (
<SquiggleItem <div key={i} className="flex flex-row pt-1">
key={i} <div className="flex-none bg-slate-100 rounded-sm px-1">
expression={r} <h3 className="text-slate-400 font-mono">{i}</h3>
width={width !== undefined ? width - 20 : width} </div>
height={50} <div className="px-2 mb-2 grow ">
showTypes={showTypes} <SquiggleItem
showControls={showControls} key={i}
chartSettings={chartSettings} expression={r}
environment={environment} width={width !== undefined ? width - 20 : width}
showSummary={showSummary} height={50}
/> showTypes={showTypes}
showControls={showControls}
chartSettings={chartSettings}
environment={environment}
showSummary={showSummary}
/>
</div>
</div>
))} ))}
</VariableBox> </VariableBox>
); );
@ -169,18 +182,22 @@ const SquiggleItem: React.FC<SquiggleItemProps> = ({
return ( return (
<VariableBox heading="Record" showTypes={showTypes}> <VariableBox heading="Record" showTypes={showTypes}>
{Object.entries(expression.value).map(([key, r]) => ( {Object.entries(expression.value).map(([key, r]) => (
<div key={key}> <div key={key} className="flex flex-row pt-1">
<h3>{key}</h3> <div className="flex-none pr-2">
<SquiggleItem <h3 className="text-slate-500 font-mono">{key}:</h3>
expression={r} </div>
width={width !== undefined ? width - 20 : width} <div className="pl-2 pr-2 mb-2 grow bg-gray-50 border border-gray-100 rounded-sm">
height={height / 3} <SquiggleItem
showTypes={showTypes} expression={r}
showSummary={showSummary} width={width !== undefined ? width - 20 : width}
showControls={showControls} height={height / 3}
chartSettings={chartSettings} showTypes={showTypes}
environment={environment} showSummary={showSummary}
/> showControls={showControls}
chartSettings={chartSettings}
environment={environment}
/>
</div>
</div> </div>
))} ))}
</VariableBox> </VariableBox>
@ -207,6 +224,9 @@ const SquiggleItem: React.FC<SquiggleItemProps> = ({
case "lambda": case "lambda":
return ( return (
<VariableBox heading="Function" showTypes={showTypes}> <VariableBox heading="Function" showTypes={showTypes}>
<div className="text-amber-700 bg-amber-100 rounded-md font-mono p-1 pl-2 mb-3 mt-1 text-sm">{`function(${expression.value.parameters.join(
","
)})`}</div>
<FunctionChart <FunctionChart
fn={expression.value} fn={expression.value}
chartSettings={chartSettings} chartSettings={chartSettings}
@ -300,9 +320,9 @@ export const SquiggleChart: React.FC<SquiggleChartProps> = ({
); );
} else { } else {
internal = ( internal = (
<ErrorBox heading={"Parse Error"}> <ErrorAlert heading={"Parse Error"}>
{errorValueToString(expressionResult.value)} {errorValueToString(expressionResult.value)}
</ErrorBox> </ErrorAlert>
); );
} }
return internal; return internal;

View File

@ -14,7 +14,7 @@ import {
defaultImports, defaultImports,
defaultBindings, defaultBindings,
} from "@quri/squiggle-lang"; } from "@quri/squiggle-lang";
import { ErrorBox } from "./ErrorBox"; import { ErrorAlert } from "./Alert";
export interface SquiggleEditorProps { export interface SquiggleEditorProps {
/** The input string for squiggle */ /** The input string for squiggle */
@ -181,7 +181,11 @@ export let SquigglePartial: React.FC<SquigglePartialProps> = ({
height={20} height={20}
/> />
</div> </div>
{error !== null ? <ErrorBox heading="Error">{error}</ErrorBox> : <></>} {error !== null ? (
<ErrorAlert heading="Error">{error}</ErrorAlert>
) : (
<></>
)}
</div> </div>
); );
}; };

View File

@ -8,31 +8,13 @@ import { useForm, useWatch } from "react-hook-form";
import * as yup from "yup"; import * as yup from "yup";
import { yupResolver } from "@hookform/resolvers/yup"; import { yupResolver } from "@hookform/resolvers/yup";
import { defaultBindings, environment } from "@quri/squiggle-lang"; import { defaultBindings, environment } from "@quri/squiggle-lang";
import { Tab } from "@headlessui/react";
interface ShowBoxProps { import { CodeIcon } from "@heroicons/react/solid";
height: number; import { CogIcon } from "@heroicons/react/solid";
children: React.ReactNode; import { ChartSquareBarIcon } from "@heroicons/react/solid";
} import { CurrencyDollarIcon } from "@heroicons/react/solid";
import { Fragment } from "react";
const ShowBox: React.FC<ShowBoxProps> = ({ height, children }) => ( import { ErrorAlert, SuccessAlert } from "./Alert";
<div className="border border-grey-100" style={{ height: height + "px" }}>
{children}
</div>
);
interface TitleProps {
readonly maxHeight: number;
children: React.ReactNode;
}
const Display: React.FC<TitleProps> = ({ maxHeight, children }) => (
<div
className="bg-gray-50 border-l border-grey-100 p-2"
style={{ maxHeight: maxHeight + "px" }}
>
{children}
</div>
);
interface PlaygroundProps { interface PlaygroundProps {
/** The initial squiggle string to put in the playground */ /** The initial squiggle string to put in the playground */
@ -79,6 +61,27 @@ const schema = yup
showControls: yup.boolean(), showControls: yup.boolean(),
showSummary: yup.boolean(), showSummary: yup.boolean(),
showSettingsPage: yup.boolean().default(false), showSettingsPage: yup.boolean().default(false),
diagramStart: yup
.number()
.required()
.positive()
.integer()
.default(0)
.min(0),
diagramStop: yup
.number()
.required()
.positive()
.integer()
.default(10)
.min(0),
diagramCount: yup
.number()
.required()
.positive()
.integer()
.default(20)
.min(2),
}) })
.required(); .required();
@ -88,12 +91,68 @@ type InputProps = {
}; };
const InputItem: React.FC<InputProps> = ({ label, children }) => ( const InputItem: React.FC<InputProps> = ({ label, children }) => (
<div> <div className="col-span-4">
<label>{label}</label> <label className="block text-sm font-medium text-gray-600">{label}</label>
{children} <div className="mt-1">{children}</div>
</div> </div>
); );
let numberStyle =
"max-w-lg block w-full shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:max-w-xs sm:text-sm border-gray-300 rounded-md";
function classNames(...classes: string[]) {
return classes.filter(Boolean).join(" ");
}
type StyledTabProps = {
name: string;
iconName: string;
};
const StyledTab: React.FC<StyledTabProps> = ({ name, iconName }) => {
let iconStyle = (isSelected: boolean) =>
classNames(
"-ml-0.5 mr-2 h-4 w-4 ",
isSelected ? "text-slate-500" : "text-gray-400 group-hover:text-gray-900"
);
let icon = (selected: boolean) =>
({
code: <CodeIcon className={iconStyle(selected)} />,
cog: <CogIcon className={iconStyle(selected)} />,
squareBar: <ChartSquareBarIcon className={iconStyle(selected)} />,
dollar: <CurrencyDollarIcon className={iconStyle(selected)} />,
}[iconName]);
return (
<Tab key={name} as={Fragment}>
{({ selected }) => (
<button className="flex rounded-md focus:outline-none focus-visible:ring-offset-gray-100 ">
<span
className={classNames(
"p-1 pl-2.5 pr-3.5 rounded-md flex items-center text-sm font-medium",
selected
? "bg-white shadow-sm ring-1 ring-black ring-opacity-5"
: ""
)}
>
{icon(selected)}
<span
className={
selected
? "text-gray-900"
: "text-gray-600 group-hover:text-gray-900"
}
>
{name}
</span>
</span>
</button>
)}
</Tab>
);
};
let SquigglePlayground: FC<PlaygroundProps> = ({ let SquigglePlayground: FC<PlaygroundProps> = ({
initialSquiggleString = "", initialSquiggleString = "",
height = 500, height = 500,
@ -105,14 +164,6 @@ let SquigglePlayground: FC<PlaygroundProps> = ({
let [importString, setImportString] = useState("{}"); let [importString, setImportString] = useState("{}");
let [imports, setImports] = useState({}); let [imports, setImports] = useState({});
let [importsAreValid, setImportsAreValid] = useState(true); let [importsAreValid, setImportsAreValid] = useState(true);
let [diagramStart, setDiagramStart] = useState(0);
let [diagramStop, setDiagramStop] = useState(10);
let [diagramCount, setDiagramCount] = useState(20);
let chartSettings = {
start: diagramStart,
stop: diagramStop,
count: diagramCount,
};
const { register, control } = useForm({ const { register, control } = useForm({
resolver: yupResolver(schema), resolver: yupResolver(schema),
defaultValues: { defaultValues: {
@ -124,11 +175,19 @@ let SquigglePlayground: FC<PlaygroundProps> = ({
showSummary: showSummary, showSummary: showSummary,
leftSizePercent: 50, leftSizePercent: 50,
showSettingsPage: false, showSettingsPage: false,
diagramStart: 0,
diagramStop: 10,
diagramCount: 20,
}, },
}); });
const vars = useWatch({ const vars = useWatch({
control, control,
}); });
let chartSettings = {
start: Number(vars.diagramStart),
stop: Number(vars.diagramStop),
count: Number(vars.diagramCount),
};
let env: environment = { let env: environment = {
sampleCount: Number(vars.sampleCount), sampleCount: Number(vars.sampleCount),
xyPointLength: Number(vars.xyPointLength), xyPointLength: Number(vars.xyPointLength),
@ -142,80 +201,222 @@ let SquigglePlayground: FC<PlaygroundProps> = ({
setImportsAreValid(false); setImportsAreValid(false);
} }
}; };
return (
<ShowBox height={height}> let samplingSettings = (
<input type="checkbox" {...register("showSettingsPage")} /> <div className="space-y-6 p-3 max-w-xl">
<div className="columns-2"> <InputItem label="Sample Count">
<div className="break-inside-avoid"> <>
{vars.showSettingsPage ? ( <input
<> type="number"
<InputItem label="Sample Count"> {...register("sampleCount")}
<input type="number" {...register("sampleCount")} /> className={numberStyle}
</InputItem> />
<InputItem label="XYPointLength Count"> <p className="mt-2 text-sm text-gray-500">
<input type="number" {...register("xyPointLength")} /> How many samples to use for Monte Carlo simulations. This can
</InputItem> occasionally be overridden by specific Squiggle programs.
<InputItem label="Chart Height (in Pixels)"> </p>
<input type="number" {...register("chartHeight")} /> </>
</InputItem> </InputItem>
<InputItem label="Show Types"> <InputItem label="Coordinate Count (For PointSet Shapes)">
<input type="checkbox" {...register("showTypes")} /> <>
</InputItem> <input
<InputItem label="Show Controls"> type="number"
<input type="checkbox" {...register("showControls")} /> {...register("xyPointLength")}
</InputItem> className={numberStyle}
<InputItem label="Show Summary Statistics"> />
<input type="checkbox" {...register("showSummary")} /> <p className="mt-2 text-sm text-gray-500">
</InputItem> When distributions are converted into PointSet shapes, we need to
<InputItem label="Editor Width"> know how many coordinates to use.
<input </p>
type="range" </>
min="1" </InputItem>
max="100" </div>
className="slider" );
{...register("leftSizePercent")}
/> let viewSettings = (
</InputItem> <div className="space-y-6 p-3 divide-y divide-gray-200 max-w-xl">
<InputItem label="Json Editor for imports"> <div className="space-y-2">
<> <h3 className="text-lg leading-6 font-medium text-gray-900 pb-2">
<JsonEditor General Display Settings
value={importString} </h3>
onChange={getChangeJson} <InputItem label="Chart Height (in pixels)">
oneLine={false} <input
showGutter={true} type="number"
height={100} {...register("chartHeight")}
/> className={numberStyle}
{importsAreValid ? "Valid" : "Invalid"} />
</> </InputItem>
</InputItem> <div className="relative flex items-start pt-3">
</> <div className="flex items-center h-5">
) : ( <input
<CodeEditor type="checkbox"
value={squiggleString} {...register("showTypes")}
onChange={setSquiggleString} className="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 rounded"
oneLine={false}
showGutter={true}
height={height - 3}
/> />
)} </div>
</div> <div className="ml-3 text-sm">
<div> <label className="font-medium text-gray-700">
<Display maxHeight={height - 3}> Show information about displayed types.
<SquiggleChart </label>
squiggleString={squiggleString} </div>
environment={env}
chartSettings={chartSettings}
height={vars.chartHeight}
showTypes={vars.showTypes}
showControls={vars.showControls}
bindings={defaultBindings}
jsImports={imports}
showSummary={vars.showSummary}
/>
</Display>
</div> </div>
</div> </div>
</ShowBox>
<div className="space-y-2 pt-8">
<h3 className="text-lg leading-6 font-medium text-gray-900 pb-2">
Distribution Display Settings
</h3>
<div className="relative flex items-start">
<div className="flex items-center h-5">
<input
type="checkbox"
{...register("showControls")}
className="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 rounded"
/>
</div>
<div className="ml-3 text-sm">
<label className="font-medium text-gray-700">
Show toggles to adjust scale of x and y axes
</label>
</div>
</div>
<div className="relative flex items-start">
<div className="flex items-center h-5">
<input
type="checkbox"
{...register("showSummary")}
className="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 rounded"
/>
</div>
<div className="ml-3 text-sm">
<label className="font-medium text-gray-700">
Show summary statistics
</label>
</div>
</div>
</div>
<div className="space-y-2 pt-8">
<h3 className="text-lg leading-6 font-medium text-gray-900 pb-2">
Function Display Settings
</h3>
<p className="mt-2 text-sm text-gray-500">
When displaying functions of single variables that return numbers or
distributions, we need to use defaults for the x-axis. We need to
select a minimum and maximum value of x to sample, and a number n of
the number of points to sample.
</p>
<div className="pt-4 grid grid-cols-1 gap-y-4 gap-x-4">
<InputItem label="Min X Value">
<input
type="number"
{...register("diagramStart")}
className={numberStyle}
/>
</InputItem>
<InputItem label="Max X Value">
<input
type="number"
{...register("diagramStop")}
className={numberStyle}
/>
</InputItem>
<InputItem label="Points between X min and X max to sample">
<input
type="number"
{...register("diagramCount")}
className={numberStyle}
/>
</InputItem>
</div>
</div>
</div>
);
let inputVariableSettings = (
<div className="space-y-6 p-3 max-w-3xl">
<h3 className="text-lg leading-6 font-medium text-gray-900">
Import Variables from JSON
</h3>
<p className="mt-2 text-sm text-gray-500">
You can import variables from JSON into your Squiggle code. Variables
are accessed with dollar signs. For example, "timeNow" would be accessed
as "$timeNow".
</p>
<div className="border border-slate-200 mt-6 mb-2">
<JsonEditor
value={importString}
onChange={getChangeJson}
oneLine={false}
showGutter={true}
height={150}
/>
</div>
<div className="p-1 pt-2">
{importsAreValid ? (
<SuccessAlert heading="Valid Json">
<></>
</SuccessAlert>
) : (
<ErrorAlert heading="Invalid JSON">
You must use valid json in this editor.
</ErrorAlert>
)}
</div>
</div>
);
return (
<Tab.Group>
<div className=" flex-col flex">
<div className="pb-4">
<Tab.List className="p-0.5 rounded-md bg-slate-100 hover:bg-slate-200 inline-flex">
<StyledTab name="Code" iconName="code" />
<StyledTab name="Sampling Settings" iconName="cog" />
<StyledTab name="View Settings" iconName="squareBar" />
<StyledTab name="Input Variables" iconName="dollar" />
</Tab.List>
</div>
<div className="flex" style={{ height: height + "px" }}>
<div className="w-1/2">
<Tab.Panels>
<Tab.Panel>
<div className="border border-slate-200">
<CodeEditor
value={squiggleString}
onChange={setSquiggleString}
oneLine={false}
showGutter={true}
height={height - 1}
/>
</div>
</Tab.Panel>
<Tab.Panel>{samplingSettings}</Tab.Panel>
<Tab.Panel>{viewSettings}</Tab.Panel>
<Tab.Panel>{inputVariableSettings}</Tab.Panel>
</Tab.Panels>
</div>
<div className="w-1/2 p-2 pl-4">
<div style={{ maxHeight: height + "px" }}>
<SquiggleChart
squiggleString={squiggleString}
environment={env}
chartSettings={chartSettings}
height={vars.chartHeight}
showTypes={vars.showTypes}
showControls={vars.showControls}
bindings={defaultBindings}
jsImports={imports}
showSummary={vars.showSummary}
/>
</div>
</div>
</div>
</div>
</Tab.Group>
); );
}; };
export default SquigglePlayground; export default SquigglePlayground;

View File

@ -1,3 +1,4 @@
@tailwind base; @tailwind base;
@tailwind components; @tailwind components;
@tailwind utilities; @tailwind utilities;
@tailwind forms;

View File

@ -49,7 +49,7 @@
"value": 0 "value": 0
}, },
"fill": { "fill": {
"value": "#4C78A8" "value": "#739ECC"
}, },
"interpolate": { "interpolate": {
"value": "linear" "value": "linear"

View File

@ -3,5 +3,5 @@ module.exports = {
theme: { theme: {
extend: {}, extend: {},
}, },
plugins: [], plugins: [require("@tailwindcss/forms")],
}; };

View File

@ -1842,6 +1842,16 @@
dependencies: dependencies:
"@hapi/hoek" "^9.0.0" "@hapi/hoek" "^9.0.0"
"@headlessui/react@^1.6.4":
version "1.6.4"
resolved "https://registry.yarnpkg.com/@headlessui/react/-/react-1.6.4.tgz#c73084e23386bef5fb86cd16da3352c3a844bb4c"
integrity sha512-0yqz1scwbFtwljmbbKjXsSGl5ABEYNICVHZnMCWo0UtOZodo2Tpu94uOVgCRjRZ77l2WcTi2S0uidINDvG7lsA==
"@heroicons/react@^1.0.6":
version "1.0.6"
resolved "https://registry.yarnpkg.com/@heroicons/react/-/react-1.0.6.tgz#35dd26987228b39ef2316db3b1245c42eb19e324"
integrity sha512-JJCXydOFWMDpCP4q13iEplA503MQO3xLoZiKum+955ZCtHINWnx26CUxVxxFQu/uLb4LW3ge15ZpzIkXKkJ8oQ==
"@hookform/resolvers@^2.8.10": "@hookform/resolvers@^2.8.10":
version "2.8.10" version "2.8.10"
resolved "https://registry.yarnpkg.com/@hookform/resolvers/-/resolvers-2.8.10.tgz#b66d7a7848b1b1dd5b976a73fff36bb366666e7d" resolved "https://registry.yarnpkg.com/@hookform/resolvers/-/resolvers-2.8.10.tgz#b66d7a7848b1b1dd5b976a73fff36bb366666e7d"
@ -3880,6 +3890,13 @@
dependencies: dependencies:
defer-to-connect "^1.0.1" defer-to-connect "^1.0.1"
"@tailwindcss/forms@^0.5.2":
version "0.5.2"
resolved "https://registry.yarnpkg.com/@tailwindcss/forms/-/forms-0.5.2.tgz#4ef45f9916dcb37838cbe7fecdcc4ba7a7c2ab59"
integrity sha512-pSrFeJB6Bg1Mrg9CdQW3+hqZXAKsBrSG9MAfFLKy1pVA4Mb4W7C0k7mEhlmS2Dfo/otxrQOET7NJiJ9RrS563w==
dependencies:
mini-svg-data-uri "^1.2.3"
"@testing-library/dom@^8.5.0": "@testing-library/dom@^8.5.0":
version "8.13.0" version "8.13.0"
resolved "https://registry.yarnpkg.com/@testing-library/dom/-/dom-8.13.0.tgz#bc00bdd64c7d8b40841e27a70211399ad3af46f5" resolved "https://registry.yarnpkg.com/@testing-library/dom/-/dom-8.13.0.tgz#bc00bdd64c7d8b40841e27a70211399ad3af46f5"
@ -12231,6 +12248,11 @@ mini-css-extract-plugin@^2.4.5, mini-css-extract-plugin@^2.6.0:
dependencies: dependencies:
schema-utils "^4.0.0" schema-utils "^4.0.0"
mini-svg-data-uri@^1.2.3:
version "1.4.4"
resolved "https://registry.yarnpkg.com/mini-svg-data-uri/-/mini-svg-data-uri-1.4.4.tgz#8ab0aabcdf8c29ad5693ca595af19dd2ead09939"
integrity sha512-r9deDe9p5FJUPZAk3A59wGH7Ii9YrjjWw0jmw/liSbHl2CHiyXj6FcDXDu2K3TjVAXqiJdaw3xxwlZZr9E6nHg==
minimalistic-assert@^1.0.0, minimalistic-assert@^1.0.1: minimalistic-assert@^1.0.0, minimalistic-assert@^1.0.1:
version "1.0.1" version "1.0.1"
resolved "https://registry.yarnpkg.com/minimalistic-assert/-/minimalistic-assert-1.0.1.tgz#2e194de044626d4a10e7f7fbc00ce73e83e4d5c7" resolved "https://registry.yarnpkg.com/minimalistic-assert/-/minimalistic-assert-1.0.1.tgz#2e194de044626d4a10e7f7fbc00ce73e83e4d5c7"