|
| 1 | +import React, { useState } from "react"; |
| 2 | +import { navigate } from "gatsby"; |
| 3 | +import { useLocation } from "@reach/router"; |
| 4 | + |
| 5 | +import { |
| 6 | + Dialog, |
| 7 | + DialogTitle, |
| 8 | + DialogContent, |
| 9 | + DialogActions, |
| 10 | + Button, |
| 11 | + SistentThemeProvider |
| 12 | +} from "@sistent/sistent"; |
| 13 | + |
| 14 | +import { SistentLayout } from "../../sistent-layout"; |
| 15 | +import TabButton from "../../../../../reusecore/Button"; |
| 16 | +import { CodeBlock } from "../button/code-block"; |
| 17 | +import { useStyledDarkMode } from "../../../../../theme/app/useStyledDarkMode"; |
| 18 | + |
| 19 | +const dialogCodeExample = ` |
| 20 | +const [open, setOpen] = useState(false); |
| 21 | +
|
| 22 | +const handleOpen = () => setOpen(true); |
| 23 | +const handleClose = () => setOpen(false); |
| 24 | +
|
| 25 | +<Button onClick={handleOpen}>Open Dialog</Button> |
| 26 | +<Dialog open={open} onClose={handleClose}> |
| 27 | + <DialogTitle>Dialog Title</DialogTitle> |
| 28 | + <DialogContent>This is a dialog example.</DialogContent> |
| 29 | + <DialogActions> |
| 30 | + <Button onClick={handleClose}>Cancel</Button> |
| 31 | + <Button color="primary" onClick={handleClose}>Confirm</Button> |
| 32 | + </DialogActions> |
| 33 | +</Dialog> |
| 34 | +`; |
| 35 | + |
| 36 | +const DialogCode = () => { |
| 37 | + const location = useLocation(); |
| 38 | + const { isDark } = useStyledDarkMode(); |
| 39 | + const [open, setOpen] = useState(false); |
| 40 | + const [openFull, setOpenFull] = useState(false); |
| 41 | + const [openForm, setOpenForm] = useState(false); |
| 42 | + |
| 43 | + const handleOpen = () => setOpen(true); |
| 44 | + const handleClose = () => setOpen(false); |
| 45 | + |
| 46 | + return ( |
| 47 | + <SistentLayout title="Dialog Code Examples"> |
| 48 | + <div className="content"> |
| 49 | + <a id="Dialog Code"> |
| 50 | + <h2>Dialog Code Implementation</h2> |
| 51 | + </a> |
| 52 | + <p> |
| 53 | + The code section demonstrates how to use the Dialog component programmatically. We define state variables |
| 54 | + to control its visibility and bind open/close handlers to button actions. This is the typical pattern when |
| 55 | + using modal components in React applications. |
| 56 | + </p> |
| 57 | + |
| 58 | + <div className="filterBtns"> |
| 59 | + <TabButton |
| 60 | + title="Overview" |
| 61 | + className={location.pathname === "/projects/sistent/components/dialog" ? "active" : ""} |
| 62 | + onClick={() => navigate("/projects/sistent/components/dialog")} |
| 63 | + /> |
| 64 | + <TabButton |
| 65 | + title="Guidance" |
| 66 | + className={location.pathname === "/projects/sistent/components/dialog/guidance" ? "active" : ""} |
| 67 | + onClick={() => navigate("/projects/sistent/components/dialog/guidance")} |
| 68 | + /> |
| 69 | + <TabButton |
| 70 | + title="Code" |
| 71 | + className={location.pathname === "/projects/sistent/components/dialog/code" ? "active" : ""} |
| 72 | + onClick={() => navigate("/projects/sistent/components/dialog/code")} |
| 73 | + /> |
| 74 | + </div> |
| 75 | + |
| 76 | + <div className="main-content"> |
| 77 | + <p>Here is a live example of the Dialog component in action:</p> |
| 78 | + <SistentThemeProvider initialMode={isDark ? "dark" : "light"}> |
| 79 | + <Button onClick={handleOpen}>Open Dialog</Button> |
| 80 | + <Dialog open={open} onClose={handleClose}> |
| 81 | + <DialogTitle>Confirm Action</DialogTitle> |
| 82 | + <DialogContent>Do you want to proceed with this action?</DialogContent> |
| 83 | + <DialogActions> |
| 84 | + <Button onClick={handleClose}>Cancel</Button> |
| 85 | + <Button color="primary" onClick={handleClose}>Confirm</Button> |
| 86 | + </DialogActions> |
| 87 | + </Dialog> |
| 88 | + </SistentThemeProvider> |
| 89 | + |
| 90 | + <h3 style={{ marginTop: "2rem" }}>Code Snippet</h3> |
| 91 | + <CodeBlock name="dialog-basic" code={dialogCodeExample} /> |
| 92 | + |
| 93 | + {/* Full Screen Dialog */} |
| 94 | + <h3 style={{ marginTop: "3rem" }}>Full-Screen Dialog Example</h3> |
| 95 | + <SistentThemeProvider initialMode={isDark ? "dark" : "light"}> |
| 96 | + <Button onClick={() => setOpenFull(true)}>Open Full-Screen Dialog</Button> |
| 97 | + <Dialog |
| 98 | + open={openFull} |
| 99 | + onClose={() => setOpenFull(false)} |
| 100 | + style={{ width: "100%", height: "100vh", maxWidth: "none" }} |
| 101 | + > |
| 102 | + <DialogTitle>Full-Screen Dialog</DialogTitle> |
| 103 | + <DialogContent> |
| 104 | + <p>This dialog stretches to full screen. Use it when the user's full attention is needed.</p> |
| 105 | + </DialogContent> |
| 106 | + <DialogActions> |
| 107 | + <Button onClick={() => setOpenFull(false)}>Cancel</Button> |
| 108 | + <Button color="primary" onClick={() => setOpenFull(false)}>Save</Button> |
| 109 | + </DialogActions> |
| 110 | + </Dialog> |
| 111 | + </SistentThemeProvider> |
| 112 | + |
| 113 | + {/* Form Inside Dialog */} |
| 114 | + <h3 style={{ marginTop: "3rem" }}>Dialog with Form Example</h3> |
| 115 | + <SistentThemeProvider initialMode={isDark ? "dark" : "light"}> |
| 116 | + <Button onClick={() => setOpenForm(true)}>Open Form Dialog</Button> |
| 117 | + <Dialog open={openForm} onClose={() => setOpenForm(false)}> |
| 118 | + <DialogTitle>Subscribe</DialogTitle> |
| 119 | + <DialogContent> |
| 120 | + <form style={{ display: "flex", flexDirection: "column", gap: "1rem" }}> |
| 121 | + <label> |
| 122 | + Email Address: |
| 123 | + <input type="email" placeholder="you@example.com" style={{ width: "100%", padding: "0.5rem" }} /> |
| 124 | + </label> |
| 125 | + <label> |
| 126 | + Name: |
| 127 | + <input type="text" placeholder="John Doe" style={{ width: "100%", padding: "0.5rem" }} /> |
| 128 | + </label> |
| 129 | + </form> |
| 130 | + </DialogContent> |
| 131 | + <DialogActions> |
| 132 | + <Button onClick={() => setOpenForm(false)}>Cancel</Button> |
| 133 | + <Button color="primary" onClick={() => setOpenForm(false)}>Subscribe</Button> |
| 134 | + </DialogActions> |
| 135 | + </Dialog> |
| 136 | + </SistentThemeProvider> |
| 137 | + </div> |
| 138 | + </div> |
| 139 | + </SistentLayout> |
| 140 | + ); |
| 141 | +}; |
| 142 | + |
| 143 | +export default DialogCode; |
0 commit comments