Skip to content

Commit 1999050

Browse files
committed
add events docs
1 parent cd0835a commit 1999050

2 files changed

Lines changed: 71 additions & 29 deletions

File tree

docs/.vitepress/config/en.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ function sidebarGuide() {
116116
},
117117
{ text: "Index Setup", link: "/guide/webuix/index-setup" },
118118
{ text: "Config", link: "/guide/webuix/config" },
119+
{ text: "Events", link: "/guide/webuix/events" },
119120
{ text: "Sanitized Module ID's", link: "/guide/webuix/sanitized-ids" },
120121
{ text: "Shortcuts", link: "/guide/webuix/shortcuts" },
121122
],

docs/en/guide/webuix/events.md

Lines changed: 70 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,91 @@
11
# WebUI X Events
22

3-
Events to help you handle states and navigation.
3+
WebUI X offers a flexible event system that enables your modules to handle UI states, navigation, and custom actions. This system empowers developers to create interactive and responsive user experiences within their modules.
44

5-
**Availability**
5+
## Availability
66

77
> [!IMPORTANT]
8-
> This feature is in active development.
8+
> **WebUI X Events** are currently in active development.
9+
> Feature support varies by platform:
10+
>
11+
> - **MMRL**: Supported from `v33661`
12+
> - **KernelSU Next**: Not supported
13+
> - **WebUI X: Portable**: Not supported
14+
> - **SukiSU Ultra**: Not supported
915
10-
- MMRL from `v33659`
11-
- KernelSU Next ❌
12-
- WebUI X: Portable ❌
13-
- SukiSU Ultra ❌
16+
## Enabling Event Handling
1417

15-
_Enable_
18+
To enable event handling in your module, add the following options to your configuration file:
1619

1720
```jsonc
1821
{
19-
// to enable `wxBack`
20-
"backHandler": "js"
22+
// Enables default back button handling (ignored if `backInterceptor` is set)
23+
"backHandler": true,
24+
// Use JavaScript to control back button behavior
25+
"backInterceptor": "javascript"
2126
}
2227
```
2328

24-
```JavaScript
25-
// Handle incoming messages and dispatch them as events on `window`
26-
window.addEventListener("message", ({ data }) => {
27-
    console.log("Data received", data);
28-
    const event = new CustomEvent(data, { detail: data });
29-
    window.dispatchEvent(event);
29+
- **`backHandler`**: Enables the default back button handler.
30+
- **`backInterceptor`**: Allows you to intercept and handle back events in JavaScript. If both are set, `backInterceptor` takes precedence.
31+
32+
## Usage Example
33+
34+
You can listen for events using the `WXEvent` API from the `webuix` package. Below are examples for handling the back button and custom pause actions:
35+
36+
```javascript
37+
import { WXEvent } from "webuix";
38+
39+
// Initialize the event system (recommended for best compatibility)
40+
WXEvent.initialize();
41+
42+
// Handle back event for a specific element (e.g., appDetails)
43+
WXEvent.on(appDetails, "back", (event) => {
44+
if (appDetails.open) {
45+
event.stopImmediatePropagation();
46+
appDetails.open = false; // Close the details panel
47+
}
3048
});
3149

32-
// Listen for "wxBack" event
33-
window.addEventListener("wxBack", () => {
34-
    console.log("Called backEvent");
35-
    const p = prompt("Type 'leave' to close");
50+
// Handle back event at the window level
51+
WXEvent.on(window, "back", (event) => {
52+
const appDetails = document.getElementById("appDetails");
53+
54+
if (appDetails?.open) {
55+
appDetails.open = false;
56+
return;
57+
}
3658

37-
    if (p === "leave") {
38-
        webui.exit();
39-
    } else {
40-
        alert("Seems that you can't type bro");
41-
    }
59+
if (confirm("Close the app?")) {
60+
webui.exit(); // Exit the WebUI app
61+
}
4262
});
4363

44-
// Listen for "wxPause" event
45-
window.addEventListener("wxPause", () => {
46-
    console.log("Called resumeEvent");
47-
    alert("Oh, hey! You're back?");
64+
// Example: Listen for a custom pause event (replace with your own logic)
65+
WXEvent.on(window, "pause", (event) => {
66+
focusInput.focus();
67+
statusEl.textContent = 'App paused - input focused';
68+
statusEl.style.color = getCssVar('error');
4869
});
4970

71+
// Listen for resume events
72+
WXEvent.on(window, "resume", (event) => {
73+
statusEl.textContent = 'App resumed';
74+
statusEl.style.color = getCssVar('success');
75+
// Add any additional logic needed when the app resumes
76+
});
5077
```
78+
79+
### How It Works
80+
81+
- The first handler listens for the `back` event on a specific element (`appDetails`). If the panel is open, it closes it and prevents the event from propagating further.
82+
- The second handler listens for the `back` event on the `window`. If no panels are open, it prompts the user to confirm exiting the app.
83+
- The third handler demonstrates listening for a custom `pause` event, updating UI elements as needed.
84+
85+
---
86+
87+
## Notes
88+
89+
- If both `backHandler` and `backInterceptor` are set, `backInterceptor` will override the default handler.
90+
- Use `event.stopImmediatePropagation()` to prevent other handlers from executing for the same event.
91+
- The event system is designed to give you fine-grained control over navigation and state transitions in your WebUI modules.

0 commit comments

Comments
 (0)