انتقل إلى المحتوى
TopClass UI

كتلة شيفرة

شيفرة في صندوق خاص بها، تُقرأ من اليسار إلى اليمين في أي صفحة. تُعرض على الخادم، وزر النسخ هو الشيفرة الوحيدة في المتصفح.

split - الحجم لم يُقَس بعد

كتلة شيفرة

split - الحجم لم يُقَس بعد

شيفرة في صندوق خاص بها، تُقرأ من اليسار إلى اليمين في أي صفحة. تُعرض على الخادم، وزر النسخ هو الشيفرة الوحيدة في المتصفح.

تثبيت
app/invoices/page.tsx
export default async function Page() {
  const invoices = await getInvoices({ status: 'overdue', sortBy: 'dueDate', order: 'descending' })
  return <InvoiceTable items={invoices} />
}

جرّبه

showCopy
wrap

الشيفرة

code-block.tsx
import type { CodeBlockDesignProps } from 'topclassui/code-block'
import { CodeBlock } from 'topclassui/code-block'

/** The docs stage: the Try it controls hand their values in as props. One line is long, so wrap shows. */
export default function Demo(props: CodeBlockDesignProps) {
  return (
    <CodeBlock lang="tsx" title="app/invoices/page.tsx" {...props}>
      {`export default async function Page() {
  const invoices = await getInvoices({ status: 'overdue', sortBy: 'dueDate', order: 'descending' })
  return <InvoiceTable items={invoices} />
}`}
    </CodeBlock>
  )
}

مرّر الشيفرة ابنًا بصيغة template literal. لا يوجد ملوّن مدمج: مرّر العقد الملوّنة عبر highlighted.

كل الحالات، بدون تمرير الفأرة

3 حالات
app/page.tsx
export default async function Page() {
  const invoices = await getInvoices()
  return <InvoiceTable items={invoices} />
}
Idle
columns.ts
export const columns = ['number', 'customer', 'issued', 'due', 'amount', 'status', 'actions'] as const
Wrapped
app/reports/page.tsx
export default async function Page() {
  const invoices = await getInvoices()
  return <InvoiceTable items={invoices} />
}
No copy button

أو استورده من الحزمة

topclassui/code-block
import { CodeBlock } from 'topclassui/code-block'

ثبّت مرة واحدة:npm i topclassui@beta zod

أمثلة

  • الشيفرة هي children، نص واحد: تُحفظ كل مسافة وكل سطر جديد، وهذا النص هو ما ينسخه الزر.
  • title يسمّي الكتلة: تصبح الشيفرة منطقة باسمه، ويُقرأ الزر "Copy page.tsx".
  • lang يُكتب في data-lang ولا شيء غير ذلك. لتلوين الشيفرة أنشئ العقد بنفسك، وقت البناء أو في مكوّن خادم خاص بك، ومرّرها عبر highlighted؛ ويبقى الزر ينسخ children.
  • السطر الطويل يُمرَّر أفقيًا داخل الكتلة؛ وwrap يكسره بدلًا من ذلك.
  • showCopy={false} يزيل الزر، فلا ترسل الكتلة عندها أي JavaScript.
  • في مكوّن الخادم مرّر {...getLabels(locale)}: كلمات الزر تأتي من labels.common.
basic
page.tsx
export default async function Page() {
  const invoices = await getInvoices()
  return <InvoiceTable items={invoices} />
}
highlighted
sample.tsx
import { CodeBlock } from 'topclassui/code-block'

// Coloured by the caller, copied as plain text
export const sample = <CodeBlock title="page.tsx">{code}</CodeBlock>
long-line
token.ts
const token = 'a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5a1b2c3d4e5abcd'
wrap
invoice-row.tsx
export const columns = ['number', 'customer', 'issued', 'due', 'amount', 'status', 'actions'] as const satisfies readonly InvoiceColumn[]
no-copy
globals.css
:root {
  --tc-radius: 0.75rem;
}
no-header
pnpm add topclassui zod
بالعربية، من اليمين إلى اليسار
الفواتير.tsx
export default async function Page() {
  const invoices = await getInvoices()
  return <InvoiceTable items={invoices} />
}

الخصائص

خصائص CodeBlock
الخاصيةالنوعالافتراضيالحدودالوصف
children*string--The code, as a template literal: every space and line break is kept, and this is what the button copies.
highlightedReactNode--The same code as nodes the caller coloured, for example with a highlighter at build time. Rendered in place of children.
labelsLabels--The built-in strings of the copy button. Defaults to English; pass getLabels(locale).labels for another.
langstring--The language, written as data-lang for a stylesheet or a script to read. Nothing here colours it.
localestring--Accepted so {...getLabels(locale)} spreads cleanly. Nothing here formats a number, so it is not used and never reaches the element.
showCopybooleantrue-Shows a button in the header that copies the code: the one part that runs in the browser.
titleReactNode--The file name in the header. It names the code region and the copy button.
wrapbooleanfalse-Breaks a long line instead of scrolling it sideways; the code is then no tab stop.

لوحة المفاتيح وإمكانية الوصول

لوحة مفاتيح CodeBlock
المفاتيحما يفعله
Tabينتقل إلى زر النسخ، ثم إلى الشيفرة، فهي محطة Tab كي تُمرَّر بلوحة المفاتيح.
EnterSpaceعلى زر النسخ ينسخ الشيفرة؛ وتقول حالة مخفية "تم النسخ" مرة واحدة.
ArrowLeftArrowRightعلى الشيفرة يمرّر السطر الطويل أفقيًا. مع wrap لا شيء يُمرَّر ولا تكون الشيفرة محطة Tab.
  • اسم الزر لا يتغير أبدًا: علامة الصح إشارة للعين، وحالة مهذبة تقول "تم النسخ" أو "تعذّر النسخ" مرة لكل ضغطة.
  • النسخ يحتاج سياقًا آمنًا (https أو localhost)؛ وفي غيره يقول الزر "تعذّر النسخ" ويبقي أيقونته.
  • الألوان التي تمرّرها في highlighted مسؤوليتك، فتحقق من تباينها مع سطح الكتلة.

من اليمين إلى اليسار

الشيفرة دائمًا dir="ltr" وtranslate="no". أما الترويسة فتتبع اتجاه الصفحة، فيقع زر النسخ في العربية على اليسار، عند نهاية السطر.

الخادم والمتصفح

يُعرض على الخادم، والجزء التفاعلي مكوّن متصفح لا يُرسل إلا عند استخدامه.

الملفات: index.tsx, client.tsx.

على الشارة: split - الحجم لم يُقَس بعد.