Skip to content
On this page

useBase64

Category
Export Size
1.39 kB
Last Changed
8 months ago

Reactive base64 transforming. Supports plain text, buffer, files, canvas, objects, maps, sets and images.

Demo

Text Input
Base64
Buffer Input
new ArrayBuffer(1024)
Base64
File Input
Base64
Image Input
Base64

Usage

ts
import { Ref, ref } from 'vue'
import { useBase64 } from '@vueuse/core'

const text = ref('')

const { base64 } = useBase64(text)
import { Ref, ref } from 'vue'
import { useBase64 } from '@vueuse/core'

const text = ref('')

const { base64 } = useBase64(text)

If you use object, array, map or set you can provide serializer in options. Otherwise, your data will be serialized by default serializer. Objects and arrays will be transformed in string by JSON.stringify. Map and set will be transformed in object and array respectively before stringify.

Type Declarations

Show Type Declarations
typescript
export interface ToDataURLOptions {
  /**
   * MIME type
   */
  type?: string | undefined
  /**
   * Image quality of jpeg or webp
   */
  quality?: any
}
export interface UseBase64ObjectOptions<T> {
  serializer: (v: T) => string
}
export interface UseBase64Return {
  base64: Ref<string>
  promise: Ref<Promise<string>>
  execute: () => Promise<string>
}
export declare function useBase64(
  target: MaybeComputedRef<string>
): UseBase64Return
export declare function useBase64(
  target: MaybeComputedRef<Blob>
): UseBase64Return
export declare function useBase64(
  target: MaybeComputedRef<ArrayBuffer>
): UseBase64Return
export declare function useBase64(
  target: MaybeComputedRef<HTMLCanvasElement>,
  options?: ToDataURLOptions
): UseBase64Return
export declare function useBase64(
  target: MaybeComputedRef<HTMLImageElement>,
  options?: ToDataURLOptions
): UseBase64Return
export declare function useBase64<T extends Record<string, unknown>>(
  target: MaybeComputedRef<T>,
  options?: UseBase64ObjectOptions<T>
): UseBase64Return
export declare function useBase64<T extends Map<string, unknown>>(
  target: MaybeComputedRef<T>,
  options?: UseBase64ObjectOptions<T>
): UseBase64Return
export declare function useBase64<T extends Set<unknown>>(
  target: MaybeComputedRef<T>,
  options?: UseBase64ObjectOptions<T>
): UseBase64Return
export declare function useBase64<T>(
  target: MaybeComputedRef<T[]>,
  options?: UseBase64ObjectOptions<T[]>
): UseBase64Return
export interface ToDataURLOptions {
  /**
   * MIME type
   */
  type?: string | undefined
  /**
   * Image quality of jpeg or webp
   */
  quality?: any
}
export interface UseBase64ObjectOptions<T> {
  serializer: (v: T) => string
}
export interface UseBase64Return {
  base64: Ref<string>
  promise: Ref<Promise<string>>
  execute: () => Promise<string>
}
export declare function useBase64(
  target: MaybeComputedRef<string>
): UseBase64Return
export declare function useBase64(
  target: MaybeComputedRef<Blob>
): UseBase64Return
export declare function useBase64(
  target: MaybeComputedRef<ArrayBuffer>
): UseBase64Return
export declare function useBase64(
  target: MaybeComputedRef<HTMLCanvasElement>,
  options?: ToDataURLOptions
): UseBase64Return
export declare function useBase64(
  target: MaybeComputedRef<HTMLImageElement>,
  options?: ToDataURLOptions
): UseBase64Return
export declare function useBase64<T extends Record<string, unknown>>(
  target: MaybeComputedRef<T>,
  options?: UseBase64ObjectOptions<T>
): UseBase64Return
export declare function useBase64<T extends Map<string, unknown>>(
  target: MaybeComputedRef<T>,
  options?: UseBase64ObjectOptions<T>
): UseBase64Return
export declare function useBase64<T extends Set<unknown>>(
  target: MaybeComputedRef<T>,
  options?: UseBase64ObjectOptions<T>
): UseBase64Return
export declare function useBase64<T>(
  target: MaybeComputedRef<T[]>,
  options?: UseBase64ObjectOptions<T[]>
): UseBase64Return

Source

SourceDemoDocs

Contributors

Anthony Fu
丶远方
Mikhailov Nikita
wheat
jelf

Changelog

v8.9.1 on 7/8/2022
a9ccc - feat(all): use MaybeComputedRef (#1768)
v8.8.0 on 7/6/2022
1fd34 - feat: accept objects (#1706)

Released under the MIT License.