dialog-alert.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { TextAttributes } from "@opentui/core"
  2. import { useTheme } from "../context/theme"
  3. import { useDialog, type DialogContext } from "./dialog"
  4. import { useKeyboard } from "@opentui/solid"
  5. export type DialogAlertProps = {
  6. title: string
  7. message: string
  8. onConfirm?: () => void
  9. }
  10. export function DialogAlert(props: DialogAlertProps) {
  11. const dialog = useDialog()
  12. const { theme } = useTheme()
  13. useKeyboard((evt) => {
  14. if (evt.name === "return") {
  15. props.onConfirm?.()
  16. dialog.clear()
  17. }
  18. })
  19. return (
  20. <box paddingLeft={2} paddingRight={2} gap={1}>
  21. <box flexDirection="row" justifyContent="space-between">
  22. <text attributes={TextAttributes.BOLD}>{props.title}</text>
  23. <text fg={theme.textMuted}>esc</text>
  24. </box>
  25. <box paddingBottom={1}>
  26. <text fg={theme.textMuted}>{props.message}</text>
  27. </box>
  28. <box flexDirection="row" justifyContent="flex-end" paddingBottom={1}>
  29. <box
  30. paddingLeft={3}
  31. paddingRight={3}
  32. backgroundColor={theme.primary}
  33. onMouseUp={() => {
  34. props.onConfirm?.()
  35. dialog.clear()
  36. }}
  37. >
  38. <text fg={theme.selectedListItemText}>ok</text>
  39. </box>
  40. </box>
  41. </box>
  42. )
  43. }
  44. DialogAlert.show = (dialog: DialogContext, title: string, message: string) => {
  45. return new Promise<void>((resolve) => {
  46. dialog.replace(
  47. () => <DialogAlert title={title} message={message} onConfirm={() => resolve()} />,
  48. () => resolve(),
  49. )
  50. })
  51. }