eugeniodepalo / react-native-gtk4

React renderer for GTK4 using node-gtk
Mozilla Public License 2.0
57 stars 3 forks source link

Is there a way to add a custom widget? #2

Closed rodrigost23 closed 6 months ago

rodrigost23 commented 7 months ago

I was trying to use a toast from libadwaita (via @girs/adw-1), but I couldn't manage to create a React component from it. Is there a way to do such a thing using this lib?

eugeniodepalo commented 6 months ago

Hi @rodrigost23, thanks for opening this issue!

There is no way at the moment to extend the reconciler with custom widgets, but you can wrap external widgets in your own components to achieve a similar effect.

For example, to implement the Toast component, you could write something like this:

import React, { useEffect, useRef } from "react"
import { Box } from "react-native-gtk4"
import Adw from "@girs/node-adw-1"

export const Toast = () => {
    const boxRef = useRef(null)

    useEffect(() => {
        const box = boxRef.current

        if (!box) return

        const overlay = new Adw.ToastOverlay()
        const toast = new Adw.Toast({ title: "Hello, World!" })

        overlay.addToast(toast)
        box.append(overlay)

        return () => {
            box.remove(overlay)
        }
    }, [])

    return (
        <Box ref={boxRef} />
    )
}

At the moment you might run into some issues with TypeScript types because @girs/node-adw-1 uses a more recent version of @girs/node-gtk-4.0, so the Gtk.Widget signatures do not match. I'm soon going to update the Gtk version used by this package which should solve that issue.

rodrigost23 commented 6 months ago

Thank you very much! This helps a lot!