Skip to content

Commit

Permalink
update: components, select menu
Browse files Browse the repository at this point in the history
  • Loading branch information
aolyang committed Dec 28, 2024
1 parent d6ebd36 commit 0b1d4a5
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 14 deletions.
1 change: 1 addition & 0 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dependencies": {
"@emotion/cache": "^11.14.0",
"@emotion/styled": "^11.14.0",
"@mdi/js": "^7.4.47",
"@mdi/react": "^1.6.1",
"@mui/material": "^6.3.0",
"@mui/material-nextjs": "^6.3.0",
Expand Down
48 changes: 48 additions & 0 deletions docs/src/components/PlainTextSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { mdiMenuDown } from "@mdi/js"
import { Icon } from "@mdi/react"
import { ButtonBase, ClickAwayListener, Menu, MenuItem, MenuList } from "@mui/material"
import cn from "clsx"
import React, { useState } from "react"

import useMenuTrigger from "@/hooks/useMenuTrigger"

interface PlainTextSelectProps<T> {
className?: string
items: T[]
renderLabel: (value: T | null) => string
children: (value: T) => React.ReactNode
}

export default function PlainTextSelect<T>(
{ className, items, renderLabel, children }: PlainTextSelectProps<T>
) {
const [value, setValue] = useState<T | null>(null)
const { anchor, open, handleClick, handleClose } = useMenuTrigger()
const handleMenuClick = (data: T | null) => {
setValue(data)
handleClose()
}
return (
<>
<ButtonBase
className={cn("bar-menu-selector px-1", className)}
onClick={handleClick}
>
{renderLabel(value)}
<Icon path={mdiMenuDown} rotate={open ? 180 : 0} size={1} />
</ButtonBase>
<Menu open={open} anchorEl={anchor}>
<ClickAwayListener onClickAway={handleClose}>
<MenuList>
<MenuItem onClick={() => handleMenuClick(null)}>None</MenuItem>
{items.map((item, index) => (
<MenuItem key={index} onClick={() => handleMenuClick(item)}>
{children(item)}
</MenuItem>
))}
</MenuList>
</ClickAwayListener>
</Menu>
</>
)
}
16 changes: 7 additions & 9 deletions docs/src/components/editor-toolbars/FontFamily.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { MenuItem, Select } from "@mui/material"
import PlainTextSelect from "@/components/PlainTextSelect"

const fonts = ["Inter", "Roboto", "sans-serif", "serif", "monospace", "cursive", "Arial", "Helvetica", "fantasy"]

export default function FontFamily() {
return (
<Select<string>
className="w-[140px]"
size="small"
displayEmpty
renderValue={value => value || "Font Family"}
<PlainTextSelect
className="w-[120px]"
items={fonts}
renderLabel={value => value || "Font Family"}
>
<MenuItem value="">None</MenuItem>
{fonts.map(font => <MenuItem key={font} value={font}>{font}</MenuItem>)}
</Select>
{font => font}
</PlainTextSelect>
)
}
16 changes: 16 additions & 0 deletions docs/src/components/editor-toolbars/FontSize.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use client"
import PlainTextSelect from "@/components/PlainTextSelect"

const sizes = [12, 14, 15, 16, 17, 18, 20, 22, 24, 26].slice(6)

export default function FontSize() {
return (
<PlainTextSelect
className="w-[100px]"
items={sizes}
renderLabel={value => value ? value + " px" : "Font Size"}
>
{size => <span style={{ fontSize: size }}>{size} px</span>}
</PlainTextSelect>
)
}
7 changes: 5 additions & 2 deletions docs/src/components/page-index/ToolbarRich.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
"use client"
import "@/styles/toolbars.css"

import cn from "clsx"

import FontFamily from "@/components/editor-toolbars/FontFamily"
import Hr from "@/components/Hr"

import FontFamily from "../editor-toolbars/FontFamily"
import FontSize from "../editor-toolbars/FontSize"
import FormatBrush from "../editor-toolbars/FormatBrush"
import FormatClear from "../editor-toolbars/FormatClear"
import Redo from "../editor-toolbars/Redo"
import Undo from "../editor-toolbars/Undo"

// TODO fix tailwindcss dark mode
export default function ToolbarRich() {
return (
<>
Expand All @@ -30,6 +32,7 @@ export default function ToolbarRich() {
<Hr className="h-[80%] mr-3" />
<div>
<FontFamily />
<FontSize />
</div>
</div>
<div className="h-[62px]" />
Expand Down
11 changes: 9 additions & 2 deletions docs/src/content/en/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
title: "Home page"
---
import "@/components/page-index/index.css"

import Heading from "@/components/page-index/Heading"
import TiptapEditor from "@/components/page-index/TiptapEditor"
import ToolbarRich from "@/components/page-index/ToolbarRich"

<Heading/>
<Heading />

```bash
pnpm add @tiptiz/editor-icons
```
```

<TiptapEditor>
<ToolbarRich />
</TiptapEditor>
20 changes: 20 additions & 0 deletions docs/src/hooks/useMenuTrigger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type React from "react"

import { useState } from "react"

export default function useMenuTrigger() {
const [anchor, setAnchor] = useState<HTMLElement | null>(null)
const open = Boolean(anchor)

const handleClick = (e: React.MouseEvent<HTMLElement>) => {
setAnchor(e.currentTarget)
}
const handleClose = () => setAnchor(null)

return {
anchor,
open,
handleClick,
handleClose
}
}
7 changes: 7 additions & 0 deletions docs/src/styles/toolbars.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
button.bar-menu-selector {
text-transform: initial;
justify-content: space-between;
}
.bar-menu-selector > svg {
transition: 0.4s transform;
}
3 changes: 2 additions & 1 deletion docs/tailwind.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export default {
"./src/components/**/*.{js,ts,jsx,tsx,mdx}",
"./src/app/**/*.{js,ts,jsx,tsx,mdx}"
],
darkMode: ["variant", "dark (&:where(.dark *))"]
darkMode: ["variant", "dark (&:where(.dark *))"],
important: true
} satisfies Config
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0b1d4a5

Please sign in to comment.