Barcode
Generate one-dimensional barcodes from ASCII text or product numbers. The component renders SVG and exports SVG, PNG, JPEG, or WebP.
Import
import { HuiBarcode } from '@nscode/handy-ui'
import type { BarcodeExpose, BarcodeFormat, BarcodeStatus } from '@nscode/handy-ui'
Examples
Basic
<script setup lang="ts">
import { HuiBarcode } from '@nscode/handy-ui'
</script>
<template>
<HuiBarcode value="NS2026111800001" />
</template>
Formats
Code 128 accepts ASCII 0–127. Code 39 accepts uppercase letters, digits, and selected punctuation. The remaining formats accept digits only.
<script setup lang="ts">
import { HuiBarcode, HuiSpace } from '@nscode/handy-ui'
const items = [
{ format: 'CODE128', value: 'Hello-HandyUI' },
{ format: 'EAN13', value: '690123456789' },
{ format: 'EAN8', value: '9638507' },
{ format: 'UPC', value: '03600029145' },
{ format: 'CODE39', value: 'NSUI-2026' },
{ format: 'ITF14', value: '1540014128876' },
{ format: 'ITF', value: '123456' }
] as const
</script>
<template>
<HuiSpace wrap align="start" size="large">
<HuiSpace v-for="item in items" :key="item.format" direction="vertical">
<span>{{ item.format }}</span>
<HuiBarcode :format="item.format" :value="item.value" />
</HuiSpace>
</HuiSpace>
</template>
Product Numbers
EAN-13 accepts 12 or 13 digits; EAN-8 accepts 7 or 8; UPC-A accepts 11 or 12; ITF-14 accepts 13 or 14. A missing GS1 check digit is added automatically. A complete number is validated. EAN and UPC display grouped digits with extended guard bars.
<script setup lang="ts">
import { HuiBarcode, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap align="start" size="large">
<HuiSpace direction="vertical"
><span>Check digit added</span><HuiBarcode value="690123456789" format="EAN13"
/></HuiSpace>
<HuiSpace direction="vertical"
><span>Complete number validated</span><HuiBarcode value="6901234567892" format="EAN13"
/></HuiSpace>
<HuiSpace direction="vertical"
><span>Incorrect check digit</span><HuiBarcode value="6901234567891" format="EAN13"
/></HuiSpace>
</HuiSpace>
</template>
Bar Width and Height
bar-width specifies the narrowest module width. Total width depends on the content and format. height excludes text and padding.
Grouped EAN and UPC text is capped at ten times bar-width. At a bar width of 1, the default font size becomes 10 instead of 14 to keep digits clear of the guard bars.
<script setup lang="ts">
import { HuiBarcode, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill size="large">
<HuiSpace wrap align="end">
<HuiSpace v-for="width in [1, 2, 3]" :key="width" direction="vertical">
<span>bar-width={{ width }}</span
><HuiBarcode value="NS2026" :bar-width="width" />
</HuiSpace>
<HuiSpace direction="vertical">
<span>EAN-13 / bar-width=1</span>
<HuiBarcode value="690123456789" format="EAN13" :bar-width="1" />
</HuiSpace>
</HuiSpace>
<HuiSpace wrap align="end">
<HuiSpace v-for="height in [40, 60, 80]" :key="height" direction="vertical">
<span>height={{ height }}</span
><HuiBarcode value="NS2026" :height="height" />
</HuiSpace>
</HuiSpace>
</HuiSpace>
</template>
Text
text changes the caption without changing the encoded content. Custom text disables product digit groups and extended guard bars. Empty text or display-value="false" removes the text area.
<script setup lang="ts">
import { HuiBarcode, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill size="large">
<HuiSpace wrap align="start">
<HuiSpace direction="vertical"
><span>display-value=true</span><HuiBarcode value="NS2026"
/></HuiSpace>
<HuiSpace direction="vertical"
><span>display-value=false</span><HuiBarcode value="NS2026" :display-value="false"
/></HuiSpace>
<HuiSpace direction="vertical"
><span>Custom text</span><HuiBarcode value="NS2026" text="Order NS2026"
/></HuiSpace>
<HuiSpace direction="vertical"
><span>Empty text</span><HuiBarcode value="NS2026" text=""
/></HuiSpace>
</HuiSpace>
<HuiSpace wrap align="start">
<HuiSpace v-for="size in [12, 14, 18]" :key="size" direction="vertical">
<span>font-size={{ size }}</span
><HuiBarcode value="NS2026" :font-size="size" />
</HuiSpace>
</HuiSpace>
<HuiSpace wrap align="start">
<HuiSpace v-for="margin in [2, 8]" :key="margin" direction="vertical">
<span>text-margin={{ margin }}</span
><HuiBarcode value="NS2026" :text-margin="margin" />
</HuiSpace>
<HuiSpace direction="vertical"
><span>font-family=serif</span><HuiBarcode value="NS2026" font-family="serif"
/></HuiSpace>
</HuiSpace>
</HuiSpace>
</template>
Colors
Bars and text share color. The default remains black on white in dark mode. Use transparent backgrounds only over a light surface.
<script setup lang="ts">
import { HuiBarcode, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap align="start" size="large">
<HuiSpace direction="vertical"><span>Default</span><HuiBarcode value="NS2026" /></HuiSpace>
<HuiSpace direction="vertical"
><span>Custom</span><HuiBarcode value="NS2026" color="#14532d" background-color="#f0fdf4"
/></HuiSpace>
<HuiSpace direction="vertical"
><span>Transparent</span><HuiBarcode value="NS2026" background-color="transparent"
/></HuiSpace>
</HuiSpace>
</template>
Padding and Quiet Zones
Horizontal padding is at least the greater of padding and the required quiet zone. Outside product digits retain their space even when quiet zones are disabled. Keep sufficient quiet zones for scanning.
<script setup lang="ts">
import { HuiBarcode, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill size="large">
<HuiSpace wrap align="start">
<HuiSpace v-for="padding in [0, 10, 20]" :key="padding" direction="vertical">
<span>padding={{ padding }}</span
><HuiBarcode value="NS2026" :padding="padding" bordered />
</HuiSpace>
</HuiSpace>
<HuiSpace wrap align="start">
<HuiSpace direction="vertical"
><span>quiet-zone=true</span><HuiBarcode value="NS2026" :padding="0" bordered
/></HuiSpace>
<HuiSpace direction="vertical"
><span>quiet-zone=false</span
><HuiBarcode value="NS2026" :padding="0" :quiet-zone="false" bordered
/></HuiSpace>
</HuiSpace>
</HuiSpace>
</template>
Border
<script setup lang="ts">
import { HuiBarcode, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap>
<HuiSpace direction="vertical"
><span>bordered=false</span><HuiBarcode value="NS2026"
/></HuiSpace>
<HuiSpace direction="vertical"
><span>bordered=true</span><HuiBarcode value="NS2026" bordered
/></HuiSpace>
</HuiSpace>
</template>
Code 39 Checksum
The optional modulo-43 check character changes the bars only. The caption remains the original value. Other formats ignore checksum.
<script setup lang="ts">
import { HuiBarcode, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap>
<HuiSpace direction="vertical"
><span>checksum=false</span><HuiBarcode value="NSUI-2026" format="CODE39"
/></HuiSpace>
<HuiSpace direction="vertical"
><span>checksum=true</span><HuiBarcode value="NSUI-2026" format="CODE39" checksum
/></HuiSpace>
</HuiSpace>
</template>
Status and Refresh
<script setup lang="ts">
import { ref } from 'vue'
import { HuiBarcode, HuiMessage, HuiRadio, HuiRadioGroup, HuiSpace } from '@nscode/handy-ui'
import type { BarcodeStatus } from '@nscode/handy-ui'
const status = ref<BarcodeStatus>('expired')
const refresh = () => {
status.value = 'active'
HuiMessage.success('Refresh requested')
}
</script>
<template>
<HuiSpace direction="vertical">
<HuiRadioGroup v-model="status" type="button">
<HuiRadio value="active">active</HuiRadio>
<HuiRadio value="loading">loading</HuiRadio>
<HuiRadio value="expired">expired</HuiRadio>
</HuiRadioGroup>
<HuiBarcode value="NS2026" :status="status" @refresh="refresh" />
<HuiBarcode
value="NS2026"
status="expired"
expired-text="Order expired"
refresh-text="Regenerate"
@refresh="refresh"
/>
</HuiSpace>
</template>
Custom Status Slot
<script setup lang="ts">
import { HuiBarcode, HuiButton, HuiMessage, HuiSpace } from '@nscode/handy-ui'
const refresh = () => HuiMessage.info('Barcode requested')
</script>
<template>
<HuiBarcode value="NS2026" status="expired" @refresh="refresh">
<template #status="{ status, refresh: requestRefresh }">
<HuiSpace direction="vertical" align="center">
<span>{{ status === 'expired' ? 'Order barcode expired' : 'Fetching barcode' }}</span>
<HuiButton icon="refresh" size="small" @click="requestRefresh">Fetch again</HuiButton>
</HuiSpace>
</template>
</HuiBarcode>
</template>
Validation
Empty content reserves a fixed 100-module placeholder without an error. Invalid content displays a format message and logs the specific reason in development. Override the message with invalid-text.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiBarcode, HuiInput, HuiSelect, HuiSpace } from '@nscode/handy-ui'
import type { BarcodeFormat } from '@nscode/handy-ui'
const value = ref('690123456789')
const format = ref<BarcodeFormat>('EAN13')
const options = ['CODE128', 'EAN13', 'EAN8', 'UPC', 'CODE39', 'ITF14', 'ITF'].map((value) => ({
label: value,
value
}))
</script>
<template>
<HuiSpace direction="vertical">
<HuiSpace wrap>
<HuiSelect
v-model="format"
:options="options"
aria-label="Barcode format"
style="width: 160px"
/>
<HuiInput v-model="value" aria-label="Barcode content" style="width: 240px" />
</HuiSpace>
<HuiBarcode :value="value" :format="format" />
<HuiBarcode value="abc" format="EAN13" invalid-text="Enter a valid product number" />
</HuiSpace>
</template>
Download and SVG Source
Exports include the background, padding, and text, excluding borders and status overlays. Bitmap exports wait for page fonts and retain the content and style captured when called.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiBarcode, HuiButton, HuiMessage, HuiSpace, HuiTextarea } from '@nscode/handy-ui'
import type { BarcodeExpose } from '@nscode/handy-ui'
const barcode = ref<BarcodeExpose | null>(null)
const source = ref('')
const download = async (type: 'image/png' | 'image/svg+xml') => {
try {
const url = await barcode.value?.toDataURL(type, { scale: 3 })
if (!url) return
const link = document.createElement('a')
link.href = url
link.download = type === 'image/png' ? 'barcode.png' : 'barcode.svg'
link.click()
HuiMessage.success('Barcode exported')
} catch {
HuiMessage.error('Export failed; check the barcode content')
}
}
const showSource = () => {
source.value = barcode.value?.toSvgString() ?? ''
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<div><HuiBarcode ref="barcode" value="690123456789" format="EAN13" /></div>
<HuiSpace wrap>
<HuiButton icon="download" @click="download('image/png')">Download PNG</HuiButton>
<HuiButton icon="download" @click="download('image/svg+xml')">Download SVG</HuiButton>
<HuiButton @click="showSource">Show SVG source</HuiButton>
</HuiSpace>
<HuiTextarea v-model="source" readonly :rows="4" aria-label="SVG source" />
</HuiSpace>
</template>
Accessible Names
The default accessible name reads the complete number, including an automatically added check digit. aria-label takes precedence over alt, including an explicit empty string.
<script setup lang="ts">
import { HuiBarcode, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap>
<HuiBarcode value="NS2026" alt="Order NS2026" />
<HuiBarcode value="NS2026" alt="Order number" aria-label="Dispatch NS2026" />
</HuiSpace>
</template>
Props
| Prop | Description | Type | Default |
|---|---|---|---|
| value | Content; empty content shows a placeholder | string | '' |
| format | Encoding format | BarcodeFormat | 'CODE128' |
| barWidth | Narrowest module width in px; positive | number | 2 |
| height | Bar height in px, excluding text and padding; positive | number | 60 |
| padding | Padding in px; horizontal space also accommodates quiet zones and outside digits | number | 10 |
| quietZone | Preserve the minimum format-specific quiet zones | boolean | true |
| color | Bar and text color | string | '#000' |
| backgroundColor | Background including padding | string | '#FFF' |
| displayValue | Show text | boolean | true |
| text | Custom caption; disables product digit grouping | string | undefined |
| fontSize | Positive font size in px. Grouped EAN/UPC text is capped at ten times barWidth to avoid guard bars. | number | 14 |
| fontFamily | Font used for display and export | string | 'monospace' |
| textMargin | Gap between bars and text in px | number | 4 |
| checksum | Append a modulo-43 check character for Code 39 | boolean | false |
| bordered | Show border | boolean | false |
| status | Current status | BarcodeStatus | 'active' |
| expiredText | Expiration message, including an empty string | string | Locale value |
| refreshText | Refresh button text, including an empty string | string | Locale value |
| invalidText | Invalid-content message, including an empty string | string | Locale value with format name |
| alt | Accessible alternative; otherwise the complete number | string | undefined |
| ariaLabel / aria-label | Accessible name; overrides alt | string | undefined |
Events
| Event | Description | Arguments |
|---|---|---|
| refresh | Expired refresh button clicked or status-slot refresh called | None |
Slots
| Slot | Description | Scope |
|---|---|---|
| status | Custom loading / expired overlay | { status: 'loading' | 'expired', refresh: () => void } |
Methods
Access methods through a template ref typed as BarcodeExpose.
| Method | Description | Signature |
|---|---|---|
| toSvgString | SVG source; empty or invalid content returns '' | () => string |
| toDataURL | Export image; empty or invalid content rejects; invalid-content errors are named BarcodeValueError | (type?: BarcodeExportType, options?: BarcodeExportOptions) => Promise<string> |
Types
type BarcodeFormat = 'CODE128' | 'EAN13' | 'EAN8' | 'UPC' | 'CODE39' | 'ITF14' | 'ITF'
type BarcodeStatus = 'active' | 'loading' | 'expired'
type BarcodeExportType = 'image/png' | 'image/jpeg' | 'image/webp' | 'image/svg+xml'
interface BarcodeExportOptions {
scale?: number
quality?: number
}
interface BarcodeExpose {
toDataURL: (type?: BarcodeExportType, options?: BarcodeExportOptions) => Promise<string>
toSvgString: () => string
}
BarcodeProps and BarcodeEmits are also exported. Bitmap scale defaults to 2; SVG ignores scale. JPEG / WebP quality ranges from 0 to 1.