Skip to content

Commit

Permalink
feat: useDialog composable for dialog management
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Jan 17, 2025
1 parent cc1b23e commit 41e26a5
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,28 @@ watch(currentContent, (newContent) => {
update({ excerpt: "Hello, Kirby!" });
```

### useDialog

Provides methods to open different types of dialogs.

**Example:**

```ts
import { useDialog } from "kirbyuse";

const { openTextDialog, openFieldsDialog } = useDialog();

const result = await openTextDialog("Are you sure?");
console.log(result); // -> true or false

await openFieldsDialog({
info: {
type: "info",
text: "An info message",
},
});
```

### useI18n

Returns translation utility functions.
Expand Down
77 changes: 77 additions & 0 deletions src/composables/dialog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { usePanel } from "kirbyuse";

/**
* Provides methods to open different types of dialogs.
*
* @example
* ```ts
* const { openTextDialog } = useDialog()
*
* const result = await openTextDialog("Are you sure?")
* console.log(result) // -> true or false
* ```
*/
export function useDialog() {
/**
* Returns a promise that resolves when the dialog is closed.
*/
function openTextDialog(text: string) {
let result = false;

return new Promise<boolean>((resolve) => {
const panel = usePanel();

panel.dialog.open({
component: "k-text-dialog",
props: { text },
on: {
// Close event will always be triggered, even on submit
close: () => {
setTimeout(() => {
resolve(result);
}, 25);
},
submit: () => {
result = true;
panel.dialog.close();
},
},
});
});
}

/**
* Returns a promise that resolves when the dialog is closed.
*/
function openFieldsDialog(fields: Record<string, any>) {
let result = false;

return new Promise<boolean>((resolve) => {
const panel = usePanel();

panel.dialog.open({
component: "k-form-dialog",
props: {
fields,
},
on: {
// Close event will always be triggered, even on submit
close: () => {
setTimeout(() => {
resolve(result);
}, 25);
},
submit: () => {
result = true;
panel.dialog.close();
},
},
});
});
}

return {
openTextDialog,
openFieldsDialog,
};
}
2 changes: 2 additions & 0 deletions src/composables/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import { usePanel } from "./panel";
* In most cases, use `window.panel.t` for Kirby's built-in translation function. This composable is useful for custom translation objects, such as those returned by a section's `label` property.
*
* @example
* ```ts
* const { t } = useI18n()
*
* // Simple string
* t("Hello") // -> "Hello"
*
* // Translation object
* t({ en: "Hello", de: "Hallo" }) // -> Returns value based on current Panel language
* ```
*/
export function useI18n() {
const panel = usePanel();
Expand Down
1 change: 1 addition & 0 deletions src/composables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from "./app";
export * from "./block";
export * from "./compatibility";
export * from "./content";
export * from "./dialog";
export * from "./i18n";
export * from "./panel";
export * from "./section";
Expand Down

0 comments on commit 41e26a5

Please sign in to comment.