Skip to content

Commit

Permalink
feat(callout): Move Callout component to nextra/components (shuding#1864
Browse files Browse the repository at this point in the history
)

Co-authored-by: Dimitri POSTOLOV <en3m@ya.ru>
Co-authored-by: Dimitri POSTOLOV <dmytropostolov@gmail.com>
  • Loading branch information
3 people committed May 22, 2023
1 parent e024e6e commit 1f9dd6d
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"@mdx-js/mdx": "^2.3.0",
"@mdx-js/react": "^2.3.0",
"@napi-rs/simple-git": "^0.1.8",
"clsx": "^1.2.1",
"github-slugger": "^2.0.0",
"graceful-fs": "^4.2.10",
"gray-matter": "^4.0.3",
Expand Down
7 changes: 4 additions & 3 deletions src/components/button.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import type { ComponentProps, ReactElement } from 'react'
import cn from 'clsx'

export const Button = ({
children,
className = '',
className,
...props
}: ComponentProps<'button'>): ReactElement => {
return (
<button
className={[
className={cn(
'nextra-button nx-transition-all active:nx-opacity-50',
'nx-bg-primary-700/5 nx-border nx-border-black/5 nx-text-gray-600 hover:nx-text-gray-900 nx-rounded-md nx-p-1.5',
'dark:nx-bg-primary-300/10 dark:nx-border-white/10 dark:nx-text-gray-400 dark:hover:nx-text-gray-50',
className
].join(' ')}
)}
{...props}
>
{children}
Expand Down
60 changes: 60 additions & 0 deletions src/components/callout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type { ReactElement, ReactNode } from 'react'
import cn from 'clsx'

import { InformationCircleIcon } from '../icons'

const TypeToEmoji = {
default: '💡',
error: '🚫',
info: <InformationCircleIcon className="nx-mt-1" />,
warning: '⚠️'
}

type CalloutType = keyof typeof TypeToEmoji

const classes: Record<CalloutType, string> = {
default: cn(
'nx-border-orange-100 nx-bg-orange-50 nx-text-orange-800 dark:nx-border-orange-400/30 dark:nx-bg-orange-400/20 dark:nx-text-orange-300'
),
error: cn(
'nx-border-red-200 nx-bg-red-100 nx-text-red-900 dark:nx-border-red-200/30 dark:nx-bg-red-900/30 dark:nx-text-red-200'
),
info: cn(
'nx-border-blue-200 nx-bg-blue-100 nx-text-blue-900 dark:nx-border-blue-200/30 dark:nx-bg-blue-900/30 dark:nx-text-blue-200'
),
warning: cn(
'nx-border-yellow-100 nx-bg-yellow-50 nx-text-yellow-900 dark:nx-border-yellow-200/30 dark:nx-bg-yellow-700/30 dark:nx-text-yellow-200'
)
}

type CalloutProps = {
type?: CalloutType
emoji?: string | ReactElement
children: ReactNode
}

export function Callout({
children,
type = 'default',
emoji = TypeToEmoji[type]
}: CalloutProps): ReactElement {
return (
<div
className={cn(
'nextra-callout nx-overflow-x-auto nx-mt-6 nx-flex nx-rounded-lg nx-border nx-py-2 ltr:nx-pr-4 rtl:nx-pl-4',
'contrast-more:nx-border-current contrast-more:dark:nx-border-current',
classes[type]
)}
>
<div
className="nx-select-none nx-text-xl ltr:nx-pl-3 ltr:nx-pr-2 rtl:nx-pr-3 rtl:nx-pl-2"
style={{
fontFamily: '"Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"'
}}
>
{emoji}
</div>
<div className="nx-w-full nx-min-w-0 nx-leading-7">{children}</div>
</div>
)
}
9 changes: 5 additions & 4 deletions src/components/code.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import type { ComponentProps, ReactElement } from 'react'
import cn from 'clsx'

export const Code = ({
children,
className = '',
className,
...props
}: ComponentProps<'code'>): ReactElement => {
const hasLineNumbers = 'data-line-numbers' in props
return (
<code
className={[
className={cn(
'nx-border-black nx-border-opacity-[0.04] nx-bg-opacity-[0.03] nx-bg-black nx-break-words nx-rounded-md nx-border nx-py-0.5 nx-px-[.25em] nx-text-[.9em]',
'dark:nx-border-white/10 dark:nx-bg-white/10',
hasLineNumbers ? '[counter-reset:line]' : '',
hasLineNumbers && '[counter-reset:line]',
className
].join(' ')}
)}
// always show code blocks in ltr
dir="ltr"
{...props}
Expand Down
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { Button } from './button'
export { Callout } from './callout'
export { CopyToClipboard } from './copy-to-clipboard'
export { Code } from './code'
export { Pre } from './pre'
Expand Down
11 changes: 6 additions & 5 deletions src/components/pre.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { useCallback, useRef } from 'react'
import { CopyToClipboard } from './copy-to-clipboard'
import { Button } from './button'
import { WordWrapIcon } from '../icons'
import cn from 'clsx'

export const Pre = ({
children,
className = '',
className,
hasCopyCode,
filename,
...props
Expand Down Expand Up @@ -34,23 +35,23 @@ export const Pre = ({
</div>
)}
<pre
className={[
className={cn(
'nx-bg-primary-700/5 nx-mb-4 nx-overflow-x-auto nx-rounded-xl nx-font-medium nx-subpixel-antialiased dark:nx-bg-primary-300/10 nx-text-[.9em]',
'contrast-more:nx-border contrast-more:nx-border-primary-900/20 contrast-more:nx-contrast-150 contrast-more:dark:nx-border-primary-100/40',
filename ? 'nx-pt-12 nx-pb-4' : 'nx-py-4',
className
].join(' ')}
)}
ref={preRef}
{...props}
>
{children}
</pre>
<div
className={[
className={cn(
'nx-opacity-0 nx-transition [div:hover>&]:nx-opacity-100 focus-within:nx-opacity-100',
'nx-flex nx-gap-1 nx-absolute nx-m-[11px] nx-right-0',
filename ? 'nx-top-8' : 'nx-top-0'
].join(' ')}
)}
>
<Button
onClick={toggleWordWrap}
Expand Down

0 comments on commit 1f9dd6d

Please sign in to comment.