Tauri Controls

repository·master·Indexed 20 days ago

https://github.com/agmmnn/tauri-controls

A library providing native-looking window controls (minimize, maximize, close) for Tauri 2 applications. It offers components designed to mimic Windows UI 3, macOS, and GNOME system designs, with dedicated packages for React, Svelte, Solid.js, and Vue.js. Key components include WindowTitlebar for full titlebar management and WindowControls for standalone buttons, utilizing Tauri's JS/TS APIs and plugins like tauri-plugin-window and tauri-plugin-os.

Tokens
11.3K
Snippets
53
Records
59
Agent score
69%

What's inside tauri-controls

  1. Install Tauri Controls for your framework

    master

    Install the specific package for your frontend framework using bun.

    • React: tauri-controls
    • Svelte: @tauri-controls/svelte
    • Solid.js: @tauri-controls/solid
    • Vue.js: @tauri-controls/vue
    # React
    bun add tauri-controls
    
    # Svelte
    bun add @tauri-controls/svelte
    
    # Solid.js
    bun add @tauri-controls/solid
    
    # Vue.js
    bun add @tauri-controls/vue
  2. Install peer dependencies and Tauri plugins

    master

    Tauri Controls requires several peer dependencies and specific Tauri plugins to function correctly.

    1. Frontend Peer Dependencies

    Install the following packages:

    bun add @tauri-apps/plugin-os @tauri-apps/api
    bun add -D clsx tailwind-merge

    2. Svelte Tailwind Configuration

    If using Svelte, you must add the following path to the content section of your tailwind.config.js to ensure styles are applied:

    "./node_modules/@tauri-controls/svelte/**/*.{js,svelte,ts}"

    3. Rust/Tauri Backend Setup

    Add the required plugins to your src-tauri directory:

    cargo add tauri-plugin-window tauri-plugin-os

    Then, register them in your main.rs file:

    fn main() {
        tauri::Builder::default()
            .plugin(tauri_plugin_os::init())
            .plugin(tauri_plugin_window::init())
            .run(tauri::generate_context!())
            .expect("error while running tauri application");
    }
    bun add @tauri-apps/plugin-os @tauri-apps/api
    bun add -D clsx tailwind-merge
    
    cargo add tauri-plugin-window tauri-plugin-os
  3. Configure WindowTitlebar control placement with controlsOrder

    master

    The controlsOrder prop in WindowTitlebar controls the layout of the window controls relative to the children content:

    • system: The platform-aware default. On macOS, controls are placed on the left. On other operating systems (Windows/Linux), controls are placed on the right.
    • left: Forces the WindowControls to the left side of the titlebar.
    • right: Forces the WindowControls to the right side of the titlebar.
  4. Use TauriAppWindowProvider in React

    master

    To access Tauri window controls (minimize, maximize, fullscreen, close) within a React application, wrap your component tree with the TauriAppWindowProvider. This provider manages the connection to the Tauri window API and tracks the window's maximized state.

    Note: The provider uses dynamic imports for @tauri-apps/api to ensure compatibility with SSR frameworks like Next.js, SvelteKit, or Nuxt.

    import { TauriAppWindowProvider } from "@tauri-controls/react"; // Adjust import path based on your installation
    
    function App() {
      return (
        <TauriAppWindowProvider>
          <MyWindowControls />
        </TauriAppWindowProvider>
      );
    }
  5. Use the WindowTitlebar component

    master

    The WindowTitlebar component manages the entire window titlebar area. It automatically adjusts the order of window control buttons (minimize, maximize, close) and titlebar content based on the detected operating system. This makes it ideal for cross-platform applications.

    If no platform is specified, it defaults to system detection.

    import { WindowTitlebar } from "tauri-controls"
    
    function MyTitlebar() {
      return (
        <WindowTitlebar>{/* Place your titlebar content here */}</WindowTitlebar>
      )
    }
  6. Use the WindowControls component

    master

    The WindowControls component is used when you only want to render the window control buttons (e.g., minimize, maximize, close) without the titlebar area. This is useful when you are building a custom titlebar and only need the buttons to be placed in a specific location.

    import { WindowControls } from "tauri-controls"
    
    function MyTitlebar() {
      return <WindowControls />
    }
  7. Configure WindowTitlebar options

    master

    The WindowTitlebar component accepts the following options:

    • controlsOrder?: "right" | "left" | "platform" | "system": Specifies the order of window controls.
      • platform: Uses OS-based positioning specified in windowControlsProps.
      • system: Automatically detects the platform and positions controls accordingly (default).
    • windowControlsProps?: WindowControlsProps: Additional props to pass down to the underlying WindowControls component.
  8. Configure WindowControls options

    master

    The WindowControls component accepts the following options:

    • platform?: "windows" | "macos" | "gnome": Specifies which platform's window controls to display. If omitted, the library automatically detects the OS.
    • justify?: boolean: If true, WindowControls will justify/snap in the flexbox where it is located.
    • hide?: boolean: If true, the window controls will be hidden.
    • hideMethod?: "display" | "visibility": Determines how the window controls are hidden (using CSS display or visibility).

    You can also pass standard HTML attributes like data-tauri-drag-region to enhance window dragging behavior.

  9. Use platform-specific window controls in Solid.js

    master

    The tauri-controls package provides platform-specific components to render window controls (like minimize, maximize, and close buttons) that match the user's operating system. You can import the appropriate component based on the target platform:

    • Windows: For Windows-style controls.
    • MacOS: For macOS-style controls.
    • Gnome: For Linux (Gnome) style controls.

    These are exported from the main controls entrypoint.

    import { Windows, MacOS, Gnome } from "tauri-controls/controls";
    
    // Usage depends on the detected platform
    function MyWindowControls() {
      return <Windows />;
    }