Skip to content

Commit

Permalink
[form-builder] Fix menus in PTE
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuslundgard authored and rexxars committed Oct 6, 2020
1 parent 1007f1e commit 9f4d1b2
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 201 deletions.
6 changes: 6 additions & 0 deletions packages/@sanity/form-builder/src/@types/parts.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ declare module 'part:@sanity/components/avatar' {
export * from '@sanity/components/src/avatar'
}

declare module 'part:@sanity/components/buttons/default-style'
declare module 'part:@sanity/components/buttons/default' {
export * from '@sanity/components/src/buttons/DefaultButton'
export {default} from '@sanity/components/src/buttons/DefaultButton'
}

declare module 'part:@sanity/components/fieldsets/default' {
export default class DefaultFieldset extends React.PureComponent<any> {
focus: () => void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default class InvalidValue extends React.PureComponent<InvalidValueProps,
<DefaultButton color="danger" onClick={this.handleAction}>
{resolution.action}
</DefaultButton>
<DefaultButton color="secondary" onClick={this.handleIgnore}>
<DefaultButton kind="secondary" onClick={this.handleIgnore}>
Ignore
</DefaultButton>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
display: flex;
background-color: var(--component-bg);
border-bottom: 1px solid var(--hairline-color);
z-index: 1;
z-index: 2;

@nest .fullscreenPortal & {
box-sizing: border-box;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,10 @@
@import 'part:@sanity/base/theme/variables-style';

.menuItem {
composes: item from 'part:@sanity/base/theme/layout/selectable-style';

-webkit-font-smoothing: inherit;
display: block;
appearance: none;
font: inherit;
background: none;
color: none;
border: 0;
border-radius: 0;
text-align: left;
width: 100%;
box-sixing: border-box;
padding: var(--small-padding) calc(var(--medium-padding) - var(--small-padding));
margin: 0;
display: flex;
align-items: center;
}

.iconContainer {
display: block;

@nest & svg {
display: block;
font-size: calc(17 / 16 * 1em);

@nest &[data-sanity-icon] {
font-size: calc(25 / 16 * 1em);
margin: calc(4 / 25 * -1em);
}
@nest & > span > span {
justify-content: flex-start;
}
}

.title {
display: block;
margin: -2px 0 -1px var(--small-padding);
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */
/* eslint-disable react/jsx-handler-names */
/* eslint-disable react/jsx-no-bind */
/* eslint-disable react/no-multi-comp */

import ToggleButton from 'part:@sanity/components/toggles/button'
import React from 'react'
import Button from 'part:@sanity/components/buttons/default'
// import ToggleButton from 'part:@sanity/components/toggles/button'
import React, {useCallback} from 'react'
import {PortableTextEditor, usePortableTextEditor} from '@sanity/portable-text-editor'
import {OverflowMenu} from './OverflowMenu'
import {PTEToolbarAction, PTEToolbarActionGroup} from './types'

import styles from './ActionMenu.css'
import {
PortableTextEditor,
usePortableTextEditor,
usePortableTextEditorSelection
} from '@sanity/portable-text-editor'

interface Props {
disabled: boolean
Expand All @@ -25,15 +17,19 @@ function ActionButton(props: {action: PTEToolbarAction; disabled: boolean; visib
const {action, disabled, visible} = props
const title = action.hotkeys ? `${action.title} (${action.hotkeys.join('+')})` : action.title

const handleClick = useCallback(() => {
action.handle()
}, [action])

return (
<ToggleButton
<Button
aria-hidden={!visible}
data-visible={visible}
disabled={disabled}
icon={action.icon}
kind="simple"
padding="small"
onClick={action.handle}
onClick={handleClick}
tabIndex={visible ? 0 : -1}
selected={action.active}
title={title}
Expand All @@ -45,19 +41,23 @@ function ActionMenuItem(props: {action: PTEToolbarAction; disabled: boolean; onC
const {action, disabled, onClose} = props
const title = action.hotkeys ? `${action.title} (${action.hotkeys.join('+')})` : action.title

const handleClick = useCallback(() => {
action.handle()
onClose()
}, [action])

return (
<button
<Button
bleed
className={styles.menuItem}
disabled={disabled}
onClick={() => {
action.handle()
onClose()
}}
type="button"
icon={action.icon}
kind="simple"
onClick={handleClick}
selected={action.active}
>
<span className={styles.iconContainer}>{React.createElement(action.icon)}</span>
<span className={styles.title}>{title}</span>
</button>
{title}
</Button>
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,10 @@
}

.menuItem {
composes: item from 'part:@sanity/base/theme/layout/selectable-style';

-webkit-font-smoothing: inherit;
display: block;
appearance: none;
font: inherit;
background: none;
color: none;
border: 0;
border-radius: 0;
text-align: left;
width: 100%;
box-sixing: border-box;
padding: var(--small-padding) calc(var(--medium-padding) - var(--extra-small-padding));
margin: 0;
display: flex;
align-items: center;
white-space: nowrap;
}

.iconContainer {
display: block;

@nest & svg {
display: block;
font-size: calc(17 / 16 * 1em);

@nest &[data-sanity-icon] {
font-size: calc(25 / 16 * 1em);
margin: calc(4 / 25 * -1em);
}
@nest & > span > span {
justify-content: flex-start;
}
}

.title {
display: block;
margin: -2px 0 -1px var(--small-padding);
}
Original file line number Diff line number Diff line change
@@ -1,83 +1,88 @@
/* eslint-disable react/jsx-handler-names */
/* eslint-disable react/jsx-no-bind */

import PlusIcon from 'part:@sanity/base/plus-icon'
import Button from 'part:@sanity/components/buttons/default'
import React from 'react'
import {Popover} from 'part:@sanity/components/popover'
import styles from './InsertMenu.css'
import Button, {ButtonProps} from 'part:@sanity/components/buttons/default'
import {MenuButton} from 'part:@sanity/components/menu-button'
import React, {useCallback} from 'react'
import {BlockItem} from './types'

type Props = {
import styles from './InsertMenu.css'

interface InsertMenuProps {
disabled: boolean
items: BlockItem[]
readOnly: boolean
}

export default function InsertMenu(props: Props) {
export default function InsertMenu(props: InsertMenuProps) {
const {disabled, items, readOnly} = props
const [open, setOpen] = React.useState(false)

const handleOpen = () => {
setOpen(true)
}

const handleClose = () => {
const handleClose = useCallback(() => {
setOpen(false)
}
}, [])

const menu = (
<div className={styles.menu}>
{items.map(item => (
<InsertMenuItem item={item} onClick={handleClose} key={item.key} />
))}
</div>
)

return (
<div className={styles.root}>
<Popover
className={styles.initialValueMenuPopover}
placement="bottom"
<MenuButton
buttonProps={{
'aria-label': 'Insert elements',
'aria-haspopup': 'menu',
'aria-expanded': open,
'aria-controls': 'insertmenu',
disabled: disabled || readOnly,
icon: PlusIcon,
kind: 'simple',
padding: 'small',
selected: open,
title: 'Insert elements'
}}
menu={menu}
open={open}
content={
(
<div className={styles.menu}>
{items.map(item => {
const itemIsDisabled = item.disabled
const title = item.type.title || item.type.type.name
return (
<button
aria-label={`Insert ${title}${item.inline ? ' (inline)' : ' (block)'}`}
disabled={itemIsDisabled}
className={styles.menuItem}
key={item.key}
onClick={() => {
item.handle()
handleClose()
}}
title={`Insert ${title}${item.inline ? ' (inline)' : ' (block)'}`}
type="button"
>
<span className={styles.iconContainer}>{React.createElement(item.icon)}</span>
<span className={styles.title}>{title}</span>
</button>
)
})}
</div>
) as any
}
>
<div>
<Button
aria-label="Insert elements"
aria-haspopup="menu"
aria-expanded={open}
aria-controls={'insertmenu'}
disabled={disabled || readOnly}
icon={PlusIcon}
kind="simple"
onClick={handleOpen}
padding="small"
selected={open}
title="Insert elements"
>
Insert
</Button>
</div>
</Popover>
placement="bottom"
setOpen={setOpen}
/>
</div>
)
}

function InsertMenuItem({
item,
onClick,
...restProps
}: {item: BlockItem} & Omit<
ButtonProps,
'aria-label' | 'children' | 'className' | 'disabled' | 'icon' | 'title' | 'type'
>) {
const handleClick = useCallback(
(event: React.MouseEvent<HTMLButtonElement>) => {
item.handle()
if (onClick) onClick(event)
},
[item]
)

const title = item.type.title || item.type.type.name

return (
<Button
{...restProps}
aria-label={`Insert ${title}${item.inline ? ' (inline)' : ' (block)'}`}
bleed
className={styles.menuItem}
disabled={item.disabled}
icon={item.icon}
kind="simple"
onClick={handleClick}
title={`Insert ${title}${item.inline ? ' (inline)' : ' (block)'}`}
>
{title}
</Button>
)
}

0 comments on commit 9f4d1b2

Please sign in to comment.