Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useBase64.md
1---2category: Utilities3---45# useBase6467Reactive base64 transforming. Supports plain text, buffer, files, canvas, objects, maps, sets and images.89## Usage1011```ts12import { useBase64 } from '@vueuse/core'13import { shallowRef } from 'vue'1415const text = shallowRef('')1617const { base64, promise, execute } = useBase64(text)18```1920### Supported Input Types2122- `string` - Plain text23- `Blob` - File or blob data24- `ArrayBuffer` - Binary data25- `HTMLCanvasElement` - Canvas element26- `HTMLImageElement` - Image element27- `Object` / `Array` / `Map` / `Set` - Serialized to JSON2829### Return Values3031| Property | Description |32| --------- | ----------------------------------------- |33| `base64` | The resulting base64 string |34| `promise` | The promise of the current transformation |35| `execute` | Manually trigger the transformation |3637### Data URL Format3839By default, the output is in Data URL format (e.g., `data:text/plain;base64,...`). Set `dataUrl: false` to get raw base64.4041```ts42const { base64 } = useBase64(text, { dataUrl: false })43// Returns raw base64 without the data URL prefix44```4546### Canvas and Image Options4748When transforming canvas or image elements, you can specify the MIME type and quality.4950```ts51const canvas = document.querySelector('canvas')5253const { base64 } = useBase64(canvas, {54type: 'image/jpeg', // MIME type55quality: 0.8, // Image quality (0-1, for jpeg/webp)56})57```5859### Custom Serializer6061For objects, arrays, maps and sets, you can provide a custom serializer. Otherwise, the data will be serialized using `JSON.stringify` (maps are converted to objects, sets to arrays).6263```ts64const data = shallowRef({ foo: 'bar' })6566const { base64 } = useBase64(data, {67serializer: v => JSON.stringify(v, null, 2),68})69```7071## Type Declarations7273```ts74export interface UseBase64Options {75/**76* Output as Data URL format77*78* @default true79*/80dataUrl?: boolean81}82export interface ToDataURLOptions extends UseBase64Options {83/**84* MIME type85*/86type?: string | undefined87/**88* Image quality of jpeg or webp89*/90quality?: any91}92export interface UseBase64ObjectOptions<T> extends UseBase64Options {93serializer?: (v: T) => string94}95export interface UseBase64Return {96base64: ShallowRef<string>97promise: ShallowRef<Promise<string>>98execute: () => Promise<string>99}100export declare function useBase64(101target: MaybeRefOrGetter<string | undefined>,102options?: UseBase64Options,103): UseBase64Return104export declare function useBase64(105target: MaybeRefOrGetter<Blob | undefined>,106options?: UseBase64Options,107): UseBase64Return108export declare function useBase64(109target: MaybeRefOrGetter<ArrayBuffer | undefined>,110options?: UseBase64Options,111): UseBase64Return112export declare function useBase64(113target: MaybeRefOrGetter<HTMLCanvasElement | undefined>,114options?: ToDataURLOptions,115): UseBase64Return116export declare function useBase64(117target: MaybeRefOrGetter<HTMLImageElement | undefined>,118options?: ToDataURLOptions,119): UseBase64Return120export declare function useBase64<T extends Record<string, unknown>>(121target: MaybeRefOrGetter<T>,122options?: UseBase64ObjectOptions<T>,123): UseBase64Return124export declare function useBase64<T extends Map<string, unknown>>(125target: MaybeRefOrGetter<T>,126options?: UseBase64ObjectOptions<T>,127): UseBase64Return128export declare function useBase64<T extends Set<unknown>>(129target: MaybeRefOrGetter<T>,130options?: UseBase64ObjectOptions<T>,131): UseBase64Return132export declare function useBase64<T>(133target: MaybeRefOrGetter<T[]>,134options?: UseBase64ObjectOptions<T[]>,135): UseBase64Return136```137