Code Block

Wrap Switch

A code block with a switch for wrapping long lines: a hidden checkbox holds the state, :has() changes the layout, no JavaScript needed.

  • code
  • wrap
  • toggle
  • no-js

Preview

1440pxHost theme

Use it with AI

  1. 1. Copy the link.
  2. 2. Write to your agent in your own words and drop the link into the sentence.
  3. 3. The agent opens the link and installs the component from the registry.

put this in the header: https://vibeui.ru/c/codeblock-008?lang=en

lib/registry.ts
const url = new URL("https://api.vibeui.ru/v1/registry/items?category=codeblock&limit=50&fields=name,title,tags")
const response = await fetch(url, { headers: { accept: "application/json" } })
Show the full instructions

What the link says, spelled out. Use it when your agent cannot open links — paste this text instead.

# Install and place "codeblock-008" (Wrap Switch) from VibeUI

## 1. Install first — do not skip, do not recreate
Run this exact command before writing any code:
npx shadcn@latest add https://vibeui.ru/r/codeblock-008.json

Registry item: https://vibeui.ru/r/codeblock-008.json
Installs to: components/vibeui/codeblock-008.tsx (the exact path follows this project's components.json aliases).
npm dependencies: none.
Registry dependencies: none.

Install it from the registry. Do not recreate it from the description,
do not substitute a similar component from another library, and do not
rewrite it to match the project's existing style.

## 2. What it is
A code block with a switch for wrapping long lines: a hidden checkbox holds the state, :has() changes the layout, no JavaScript needed.

A code block whose long-line wrapping is toggled from the header. State lives on a checkbox and :has(), zero dependencies, no client JS.

## 3. How to use it
import { Codeblock008 } from "@/components/vibeui/codeblock-008"

<Codeblock008
  title="lib/registry.ts"
  label="Wrap lines"
  wrapByDefault={false}
/>

Read the installed file for the full prop list.

## 4. Where to place it
This is a small inline component. Put it exactly where the user asked,
inside the existing markup. Do not create a new page, section or
wrapper for it. If a similar control already sits in that spot,
replace it instead of adding a second one.

Placement: ___
(The user fills this line in. If it is still blank, ask where to put it
instead of guessing.)

## 5. Keep exactly as installed
- the local --vibeui-codeblock-008-* palette — do not swap it for your theme tokens
- the checkbox as the source of state: with useState the block stops working before hydration
- hiding the input with clip-path rather than display:none — otherwise the switch leaves the tab order
- removing horizontal scrolling together with wrapping: otherwise a dead scrollbar remains
- the focus ring on the label through :has(): the input itself is invisible and shows nothing
- the dark surface of the block — it carries the contrast for the light monospaced text

## 6. You may change
- the block code through code and the heading through title
- the switch caption through label
- the initial state through wrapByDefault
- the colour of the active switch and the block width

## 7. Rules
- The choice is not remembered between pages: that needs client state and storage.
- Line numbers must not be shown while wrapping is on — wrapping breaks their correspondence.
- The :has() selector must be supported: in very old engines the block simply never wraps.
- Do not lift the CSS variables into globals.css: the component has to stay a single file.

## 8. Verify
- it renders with no console errors;
- it looks like the preview on the VibeUI page you copied this from;
- if it does not, you changed something listed in section 5 — put it back.

For developers

npx shadcn@latest add https://vibeui.ru/r/codeblock-008.json
https://vibeui.ru/r/codeblock-008.json

Компонент самодостаточен: один файл, без зависимостей, собственная палитра в локальных переменных --vibeui-codeblock-008-*. Серверный: состояние переключателя держит нативный checkbox, а корневой селектор :has(input:checked) переводит код с white-space:pre на pre-wrap и заодно снимает горизонтальную прокрутку. Чекбокс спрятан clip-path, поэтому остаётся доступным с клавиатуры; переключатель нарисован из span с ::after, обводка фокуса вешается на label через :has(input:focus-visible).

Component source

The same file your agent installs. Here in case you would rather copy it by hand.

import type { CSSProperties } from "react"

export type Codeblock008Props = {
  title?: string
  label?: string
  code?: string
  wrapByDefault?: boolean
  className?: string
  style?: CSSProperties
}

// Идея компонента: переключатель переноса длинных строк без единой строки JS.
// Состояние держит скрытый чекбокс, а :has() переводит код между
// white-space:pre и pre-wrap — переключение живёт и до гидратации.
const STYLES = `
:where([data-vibeui-block="codeblock-008"]){
--vibeui-codeblock-008-bg:oklch(0.2 0.026 250);
--vibeui-codeblock-008-head:oklch(0.24 0.03 250);
--vibeui-codeblock-008-fg:oklch(0.93 0.008 250);
--vibeui-codeblock-008-muted:oklch(0.68 0.018 250);
--vibeui-codeblock-008-border:oklch(1 0 0 / 13%);
--vibeui-codeblock-008-accent:oklch(0.75 0.14 250);
--vibeui-codeblock-008-knob:oklch(0.98 0.005 250);
--vibeui-codeblock-008-mono:ui-monospace,SFMono-Regular,Menlo,Consolas,"Liberation Mono",monospace;
--vibeui-codeblock-008-font:ui-sans-serif,system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif;
}
[data-vibeui-block="codeblock-008"]{
display:flex;flex-direction:column;
width:100%;max-width:32rem;box-sizing:border-box;margin:0;overflow:hidden;
border:1px solid var(--vibeui-codeblock-008-border);border-radius:0.75rem;
background:var(--vibeui-codeblock-008-bg);color:var(--vibeui-codeblock-008-fg);
font-family:var(--vibeui-codeblock-008-font);
}
[data-vibeui-block="codeblock-008"] figcaption{
display:flex;align-items:center;justify-content:space-between;gap:0.75rem;
padding:0.4375rem 0.625rem 0.4375rem 0.875rem;
background:var(--vibeui-codeblock-008-head);
border-bottom:1px solid var(--vibeui-codeblock-008-border);
font-size:0.75rem;color:var(--vibeui-codeblock-008-muted);
}
[data-vibeui-block="codeblock-008"] [data-part="name"]{font-family:var(--vibeui-codeblock-008-mono)}
[data-vibeui-block="codeblock-008"] [data-part="toggle"]{
display:inline-flex;align-items:center;gap:0.5rem;cursor:pointer;
padding:0.1875rem 0.375rem;border-radius:0.5rem;
font-size:0.75rem;user-select:none;-webkit-user-select:none;
}
[data-vibeui-block="codeblock-008"] input{
position:absolute;width:1px;height:1px;margin:-1px;padding:0;
border:0;clip-path:inset(50%);overflow:hidden;white-space:nowrap;
}
[data-vibeui-block="codeblock-008"] [data-part="track"]{
position:relative;flex:none;width:1.875rem;height:1.0625rem;border-radius:999px;
background:oklch(1 0 0 / 16%);
transition:background-color .18s ease;
}
[data-vibeui-block="codeblock-008"] [data-part="track"]::after{
content:"";position:absolute;top:0.1875rem;left:0.1875rem;
width:0.6875rem;height:0.6875rem;border-radius:999px;
background:var(--vibeui-codeblock-008-knob);
transition:transform .18s ease;
}
[data-vibeui-block="codeblock-008"] [data-part="toggle"]:has(input:checked){color:var(--vibeui-codeblock-008-fg)}
[data-vibeui-block="codeblock-008"] [data-part="toggle"]:has(input:checked) [data-part="track"]{background:var(--vibeui-codeblock-008-accent)}
[data-vibeui-block="codeblock-008"] [data-part="toggle"]:has(input:checked) [data-part="track"]::after{transform:translateX(0.8125rem)}
[data-vibeui-block="codeblock-008"] [data-part="toggle"]:has(input:focus-visible){outline:2px solid var(--vibeui-codeblock-008-accent);outline-offset:2px}
[data-vibeui-block="codeblock-008"] pre{margin:0;padding:0.875rem;overflow-x:auto}
[data-vibeui-block="codeblock-008"] code{
display:block;
font-family:var(--vibeui-codeblock-008-mono);
font-size:0.8125rem;line-height:1.65;white-space:pre;
}
/* Перенос включается отмеченным чекбоксом: подпись меняет саму раскладку. */
[data-vibeui-block="codeblock-008"]:has(input:checked) code{
white-space:pre-wrap;overflow-wrap:anywhere;
}
[data-vibeui-block="codeblock-008"]:has(input:checked) pre{overflow-x:hidden}
@media (prefers-reduced-motion:reduce){[data-vibeui-block="codeblock-008"] *{animation:none!important;transition:none!important}}
`

const CODE = `const url = new URL("https://api.vibeui.ru/v1/registry/items?category=codeblock&limit=50&fields=name,title,tags")
const response = await fetch(url, { headers: { accept: "application/json" } })`

/** Блок кода с переключателем переноса длинных строк, без JS. */
export function Codeblock008({
  title = "lib/registry.ts",
  label = "Переносить строки",
  code = CODE,
  wrapByDefault = false,
  className,
  style,
}: Codeblock008Props) {
  return (
    <>
      <style href="vibeui-codeblock-008" precedence="medium">
        {STYLES}
      </style>
      <figure
        data-vibeui-block="codeblock-008"
        className={className}
        style={style}
      >
        <figcaption>
          <span data-part="name">{title}</span>
          <label data-part="toggle">
            <input type="checkbox" defaultChecked={wrapByDefault} />
            <span data-part="track" aria-hidden="true" />
            {label}
          </label>
        </figcaption>
        <pre>
          <code>{code}</code>
        </pre>
      </figure>
    </>
  )
}