Liquid Glass Vue

repository·main·Indexed 42 days ago

https://github.com/wxperia/liquid-glass-vue

Apple-style liquid glass visual effects for Vue 3 applications, featuring refraction, chromatic aberration, and elastic movement. Version 1.0.9 provides the <LiquidGlass> component, a directive for existing elements, and advanced shader utilities including ShaderDisplacementGenerator for worker-based shader computation.

Tokens
5.6K
Snippets
21
Records
26
Agent score
54%

What's inside Liquid Glass Vue

  1. When to use the Directive vs the Component

    main

    Choose between the Directive and the Component based on your layout requirements:

    Use the Directive when:

    • You want to apply the liquid glass effect to existing elements.
    • You need more control over the DOM structure.
    • You're working with complex layouts where components might interfere.
    • You want to apply the effect to third-party components.

    Use the Component when:

    • You want a complete glass container solution.
    • You prefer the component-based approach.
    • You need the full feature set with minimal setup.
    • You're building new UI elements from scratch.
  2. When to use the LiquidGlass component vs. Directive

    main

    Use a Directive when:

    • You want to apply the liquid glass effect to an existing element.
    • You need more control over the DOM structure.
    • You are dealing with complex layouts that might interfere with the component.
    • You want to apply the effect to third-party components.

    Use the Component when:

    • You want a complete glass container solution.
    • You prefer a component-based approach.
    • You need the full feature set with minimal setup.
    • You are building new UI elements from scratch.
  3. Use the LiquidGlass component

    main

    You can use the <LiquidGlass> component as a wrapper for any content. It provides the liquid glass visual effect to its children.

    Basic Usage

    <script setup lang="ts">
      import { LiquidGlass } from 'liquid-glass-vue'
    </script>
    
    <template>
      <LiquidGlass>
        <div class="p-6">
          <h2>Your content here</h2>
          <p>This will have the liquid glass effect</p>
        </div>
      </LiquidGlass>
    </template>
    <script setup lang="ts">
      import { LiquidGlass } from 'liquid-glass-vue'
    </script>
    
    <template>
      <LiquidGlass>
        <div class="p-6">
          <h2 class="p-6">您的内容在这里</h2>
          <p>这将拥有液体玻璃效果</p>
        </div>
      </LiquidGlass>
    </template>
  4. Make LiquidGlass respond to a parent container's mouse movement

    main

    By default, the glass effect responds to mouse movement over the component itself. To make the effect respond to mouse movement over a larger area (like a parent container), pass a reference to that container via the mouseContainer prop.

    <script setup lang="ts">
      import { ref } from 'vue'
      import { LiquidGlass } from 'liquid-glass-vue'
    
      const containerRef = ref<HTMLDivElement>()
    </script>
    
    <template>
      <div
        ref="containerRef"
        class="w-full h-screen bg-image"
      >
        <LiquidGlass
          :mouse-container="containerRef"
          :elasticity="0.3"
          :style="{ position: 'fixed', top: '50%', left: '50%' }"
        >
          <div class="p-6">
            <h2 class="text-white">Glass responds to mouse anywhere in the container</h2>
          </div>
        </LiquidGlass>
      </div>
    </template>
  5. Register LiquidGlass globally in Vue 3

    main

    To use the <LiquidGlass> component anywhere in your application without manual imports, register it as a plugin in your main entry file.

    import LiquidGlass from 'liquid-glass-vue'
    import { createApp } from 'vue'
    
    const app = createApp()
    
    app.use(LiquidGlass)
  6. Use the LiquidGlass component

    main

    Wrap your content with the <LiquidGlass> component to apply the glass effect. The component supports arbitrary child elements.

    <script setup lang="ts">
      import { LiquidGlass } from 'liquid-glass-vue'
    </script>
    
    <template>
      <LiquidGlass>
        <div class="p-6">
          <h2 class="text-white">Your content here</h2>
          <p>This will have the liquid glass effect</p>
        </div>
      </LiquidGlass>
    </template>
  7. Register LiquidGlass globally in Vue

    main

    To use the <LiquidGlass> component throughout your entire application without manual imports, register it as a plugin in your main entry file.

    import LiquidGlass from 'liquid-glass-vue'
    import { createApp } from 'vue'
    
    const app = createApp()
    
    app.use(LiquidGlass)
  8. Install Liquid Glass Vue as a Vue plugin

    main

    You can install Liquid Glass Vue globally in your Vue application by using the default export's install method. This registers the LiquidGlass component globally, allowing you to use it anywhere in your application without manual imports.

    import { createApp } from 'vue'
    import LiquidGlassPlugin from '@wxperia/liquid-glass-vue'
    
    const app = createApp(App)
    app.use(LiquidGlassPlugin)
    app.mount('#app')
  9. Configure LiquidGlass with props

    main

    The <LiquidGlass> component accepts several props to fine-tune the visual effect, including displacement, blur, saturation, and elasticity.

    Example: Highly customized button

    <script setup lang="ts">
      import { LiquidGlass } from '@wxperia/liquid-glass-vue'
    
      const handleClick = () => {
        console.log('Button clicked!')
      }
    </script>
    
    <template>
      <LiquidGlass
        :displacement-scale="64"
        :blur-amount="0.1"
        :saturation="130"
        :aberration-intensity="2"
        :elasticity="0.35"
        :corner-radius="100"
        padding="8px 16px"
        @click="handleClick"
        :effect="mosaicGlass"
        :mode="'shader'"
      >
        <span class="text-white font-medium">Click me</span>
      </LiquidGlass>
    </template>
    <script setup lang="ts">
      import { LiquidGlass } from '@wxperia/liquid-glass-vue'
    
      const handleClick = () => {
        console.log('按钮被点击!')
      }
    </script>
    
    <template>
      <LiquidGlass
        :displacement-scale="64"
        :blur-amount="0.1"
        :saturation="130"
        :aberration-intensity="2"
        :elasticity="0.35"
        :corner-radius="100"
        padding="8px 16px"
        @click="handleClick"
        :effect="mosaicGlass"
        :mode="'shader'"
      >
        <span class="text-white font-medium">点击我</span>
      </LiquidGlass>
    </template>
  10. Configure LiquidGlass with props

    main

    You can customize the visual properties of the glass effect using props such as displacementScale, blurAmount, saturation, and elasticity. For advanced shader effects, set the mode to 'shader' and choose an effect type.

    <script setup lang="ts">
      import { LiquidGlass } from '@wxperia/liquid-glass-vue'
    
      const handleClick = () => {
        console.log('Button clicked!')
      }
    </script>
    
    <template>
      <LiquidGlass
        :displacement-scale="64"
        :blur-amount="0.1"
        :saturation="130"
        :aberration-intensity="2"
        :elasticity="0.35"
        :corner-radius="100"
        padding="8px 16px"
        @click="handleClick"
        :effect="mosaicGlass"
        :mode="'shader'"
      >
        <span class="text-white font-medium">Click Me</span>
      </LiquidGlass>
    </template>