Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

Update button component to support different states and intents, and add corresponding stories and tests (#30)

Myles Scolnick563a5164b52a
diff --git a/frontend/src/components/ui/button.tsx b/frontend/src/components/ui/button.tsx
index 3974fd777a..41a96e3028 100644
--- a/frontend/src/components/ui/button.tsx
+++ b/frontend/src/components/ui/button.tsx
@@ -17,8 +17,24 @@ const buttonVariants = cva(
activeCommon
),
destructive: cn(
- "bg-destructive text-destructive-foreground shadow-smSolid",
- "hover:bg-destructive/90 border border-destructive",
+ "border shadow-smSolid",
+ "bg-[var(--red-9)] hover:bg-[var(--red-10)]",
+ "text-[var(--red-1)]",
+ "border-[var(--red-11)]",
+ activeCommon
+ ),
+ success: cn(
+ "border shadow-smSolid",
+ "bg-[var(--grass-9)] hover:bg-[var(--grass-10)]",
+ "text-[var(--grass-1)]",
+ "border-[var(--grass-11)]",
+ activeCommon
+ ),
+ warn: cn(
+ "border shadow-smSolid",
+ "bg-[var(--yellow-9)] hover:bg-[var(--yellow-10)]",
+ "text-[var(--yellow-12)]",
+ "border-[var(--yellow-11)]",
activeCommon
),
action: cn(
diff --git a/frontend/src/plugins/impl/ButtonPlugin.tsx b/frontend/src/plugins/impl/ButtonPlugin.tsx
index 804c530c75..03a074be8c 100644
--- a/frontend/src/plugins/impl/ButtonPlugin.tsx
+++ b/frontend/src/plugins/impl/ButtonPlugin.tsx
@@ -4,23 +4,56 @@ import { z } from "zod";
import { IPlugin, IPluginProps } from "../types";
import { Button } from "../../components/ui/button";
import { renderHTML } from "../core/RenderHTML";
+import { Intent, zodIntent } from "./common/intent";
-export class ButtonPlugin implements IPlugin<number, { label: string }> {
+interface Data {
+ label: string;
+ kind: Intent;
+ disabled: boolean;
+}
+
+export class ButtonPlugin implements IPlugin<number, Data> {
tagName = "marimo-button";
- validator = z.object({ initialValue: z.number(), label: z.string() });
+ validator = z.object({
+ label: z.string(),
+ kind: zodIntent,
+ disabled: z.boolean().default(false),
+ });
- render(props: IPluginProps<number, { label: string }>): JSX.Element {
+ render(props: IPluginProps<number, Data>): JSX.Element {
+ const {
+ data: { disabled, kind, label },
+ } = props;
// value counts number of times button was clicked
return (
<Button
- variant="secondary"
+ variant={kindToButtonVariant(kind)}
+ disabled={disabled}
size="xs"
- onClick={() => props.setValue((v) => v + 1)}
+ onClick={() => {
+ if (disabled) {
+ return;
+ }
+ props.setValue((v) => v + 1);
+ }}
type="submit"
>
- {renderHTML({ html: props.data.label })}
+ {renderHTML({ html: label })}
</Button>
);
}
}
+
+function kindToButtonVariant(kind: Intent) {
+ switch (kind) {
+ case "neutral":
+ return "secondary";
+ case "danger":
+ return "destructive";
+ case "warn":
+ return "warn";
+ case "success":
+ return "success";
+ }
+}
diff --git a/frontend/src/plugins/impl/common/intent.ts b/frontend/src/plugins/impl/common/intent.ts
new file mode 100644
index 0000000000..cbdb72301e
--- /dev/null
+++ b/frontend/src/plugins/impl/common/intent.ts
@@ -0,0 +1,8 @@
+/* Copyright 2023 Marimo. All rights reserved. */
+
+import { z } from "zod";
+
+export type Intent = "neutral" | "success" | "warn" | "danger";
+export const zodIntent = z
+ .enum(["neutral", "success", "warn", "danger"])
+ .default("neutral");
diff --git a/frontend/src/stories/button.stories.mdx b/frontend/src/stories/button.stories.mdx
index 45a9209a31..769d6d171d 100644
--- a/frontend/src/stories/button.stories.mdx
+++ b/frontend/src/stories/button.stories.mdx
@@ -11,6 +11,14 @@ import { Mail, Trash2Icon, Loader2 } from "lucide-react";
<Button>Button</Button>
</Story>
+<Story name="Success">
+ <Button variant="sucess">Button</Button>
+</Story>
+
+<Story name="Warn">
+ <Button variant="warn">Button</Button>
+</Story>
+
<Story name="Outline">
<Button variant="outline">Button</Button>
</Story>
diff --git a/marimo/_plugins/ui/_impl/input.py b/marimo/_plugins/ui/_impl/input.py
index 06ac069a1c..ec4ccecc43 100644
--- a/marimo/_plugins/ui/_impl/input.py
+++ b/marimo/_plugins/ui/_impl/input.py
@@ -623,6 +623,12 @@ class button(UIElement[Any, Any]):
on_click=lambda value: value + 1,
label='increment'
)
+
+ # adding intent
+ delete_button = mo.ui.button(
+ label='Do not click',
+ kind='danger',
+ )
```
**Attributes.**
@@ -634,6 +640,8 @@ class button(UIElement[Any, Any]):
- `on_click`: a callable called on click that takes the current
value of the button and returns a new value
- `value`: an initial value for the button
+ - `kind`: 'neutral', 'success', 'warn', or 'danger'
+ - `disabled`: whether the button is disabled
- `label`: text label for the element
"""
@@ -643,6 +651,8 @@ class button(UIElement[Any, Any]):
self,
on_click: Optional[Callable[[Any], Any]] = None,
value: Optional[Any] = None,
+ kind: Literal["neutral", "success", "warn", "danger"] = "neutral",
+ disabled: bool = False,
label: str = "click here",
) -> None:
self._on_click = (lambda _: value) if on_click is None else on_click
@@ -652,7 +662,10 @@ class button(UIElement[Any, Any]):
# frontend's value is always a counter
initial_value=0,
label=label,
- args={},
+ args={
+ "kind": kind,
+ "disabled": disabled,
+ },
)
def _convert_value(self, value: Any) -> Any:
diff --git a/marimo/_smoke_tests/buttons.py b/marimo/_smoke_tests/buttons.py
new file mode 100644
index 0000000000..da17f27204
--- /dev/null
+++ b/marimo/_smoke_tests/buttons.py
@@ -0,0 +1,28 @@
+import marimo
+
+__generated_with = "0.1.2"
+app = marimo.App()
+
+
+def __():
+ import marimo as mo
+ return mo,
+
+
+def __(mo):
+ kinds = ["neutral", "success", "warn", "danger"]
+
+ mo.vstack([mo.ui.button(label=kind, kind=kind) for kind in kinds])
+ return kinds,
+
+
+def __(kinds, mo):
+ mo.vstack([mo.ui.button(label=kind, kind=kind, disabled=True) for kind in kinds])
+ return
+
+
+if __name__ == "__main__":
+ app.run()