liquid-glass-react

repository·master·Indexed 26 days ago

https://github.com/rdev/liquid-glass-react

A React implementation of Apple's 'Liquid Glass' visual effect, featuring refraction, chromatic aberration, and elastic movement. The library provides the LiquidGlass component for high-level usage, a GlassContainer primitive for low-level control, and a ShaderDisplacementGenerator for programmatically generating displacement maps via custom fragment shaders.

Tokens
2.7K
Snippets
7
Records
14
Agent score
89%

What's inside liquid-glass-react

  1. Make LiquidGlass respond to a parent container's mouse movement

    master

    To make the glass effect respond to mouse movement over a larger area (like a full-screen background or a large parent container), pass a ref of that container to the mouseContainer prop.

    import { useRef } from 'react'
    import LiquidGlass from 'liquid-glass-react'
    
    function App() {
      const containerRef = useRef<HTMLDivElement>(null)
    
      return (
        <div ref={containerRef} className="w-full h-screen bg-image">
          <LiquidGlass
            mouseContainer={containerRef}
            elasticity={0.3}
            style={{ position: 'fixed', top: '50%', left: '50%' }}
          >
            <div className="p-6">
              <h2 className="text-white">Glass responds to mouse anywhere in the container</h2>
            </div>
          </LiquidGlass>
        </div>
      )
    }
  2. Basic usage of LiquidGlass

    master

    Wrap any content with the LiquidGlass component to apply the glass effect to its children. By default, the effect responds to mouse movement over the component itself.

    import LiquidGlass from 'liquid-glass-react'
    
    function App() {
      return (
        <LiquidGlass>
          <div className="p-6">
            <h2 className="text-xl">Your content here</h2>
            <p>This will have the liquid glass effect</p>
          </div>
        </LiquidGlass>
      )
    }
  3. Configure LiquidGlass with custom props

    master

    You can customize the visual properties of the glass effect, such as displacement, blur, saturation, and elasticity. This is useful for creating specific UI elements like buttons.

    <LiquidGlass
      displacementScale={64}
      blurAmount={0.1}
      saturation={130}
      aberrationIntensity={2}
      elasticity={0.35}
      cornerRadius={100}
      padding="8px 16px"
      onClick={() => console.log('Button clicked!')}
    >
      <span className="text-white font-medium">Click Me</span>
    </LiquidGlass>
  4. Reference LiquidGlass props

    master

    The following props are available for the LiquidGlass component to control its visual appearance and behavior.

    | Prop | Type | Default | Description |
    |------|------|---------|-------------|
    | `children` | `React.ReactNode` | - | The content to render inside the glass container |
    | `displacementScale` | `number` | `70` | Controls the intensity of the displacement effect |
    | `blurAmount` | `number` | `0.0625` | Controls the blur/frosting level |
    | `saturation` | `number` | `140` | Controls color saturation of the glass effect |
    | `aberrationIntensity` | `number` | `2` | Controls chromatic aberration intensity |
    | `elasticity` | `number` | `0.15` | Controls the "liquid" elastic feel (0 = rigid, higher = more elastic) |
    | `cornerRadius` | `number` | `999` | Border radius in pixels |
    | `className` | `string` | `""` | Additional CSS classes |
    | `padding` | `string` | - | CSS padding value |
    | `style` | `React.CSSProperties` | - | Additional inline styles |
    | `overLight` | `boolean` | `false` | Whether the glass is over a light background |
    | `onClick` | `() => void` | - | Click handler |
    | `mouseContainer` | `React.RefObject<HTMLElement \| null> \| null` | `null` | Container element to track mouse movement on (defaults to the glass component itself) |
    | `mode` | `"standard" \| "polar" \| "prominent" \| "shader"` | `"standard"` | Refraction mode for different visual effects. `shader` is the most accurate but not the most stable. |
    | `globalMousePos` | `{ x: number; y: number }` | - | Global mouse position coordinates for manual control |
    | `mouseOffset` | `{ x: number; y: number }` | - | Mouse position offset for fine-tuning positioning |
  5. Use the LiquidGlass component

    master
    The LiquidGlass component creates a glassmorphism effect with interactive displacement, chromatic aberration, and elastic movement based on mouse position. It can track mouse movement internally or accept external mouse coordinates for synchronized effects.
  6. Configure GlassContainer for low-level control

    master
    The GlassContainer component is a lower-level primitive used to build the glass effect without the automatic elastic scaling and mouse tracking logic found in LiquidGlass. It is useful for building custom interactive glass elements.
  7. Generate displacement maps with ShaderDisplacementGenerator

    master

    The ShaderDisplacementGenerator class is used to programmatically generate data URLs representing displacement maps. This is useful for creating custom SVG filters or canvas-based refraction effects.

    Workflow:

    1. Initialize: Pass a ShaderOptions object to the constructor.
    2. Update: Call updateShader(mousePosition?) to recalculate the displacement map based on the current state. This returns a dataURL string.
    3. Cleanup: Call destroy() to remove the internal canvas from the document.

    Methods:

    • updateShader(mousePosition?: Vec2): string: Generates and returns a new displacement map as a Data URL.
    • getScale(): number: Returns the current canvasDPI.
    • destroy(): void: Cleans up the internal canvas element.