Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Settings: Show message when Visual or Code editor are disabled #52737

Merged
merged 11 commits into from Jul 31, 2023
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### Enhancements

- `ColorPalette`, `BorderControl`: Don't hyphenate hex value in `aria-label` ([#52932](https://github.com/WordPress/gutenberg/pull/52932)).
- `MenuItemsChoice`, `MenuItem`: Support a `disabled` prop on a menu item ([#52737](https://github.com/WordPress/gutenberg/pull/52737)).

### Bug Fix

Expand Down
7 changes: 7 additions & 0 deletions packages/components/src/menu-item/README.md
Expand Up @@ -34,6 +34,13 @@ MenuItem supports the following props. Any additional props are passed through t

Element to render as child of button.

### `disabled`

- Type: `boolean`
- Required: No

Refer to documentation for [Button's `disabled` prop](/packages/components/src/button/README.md#disabled-boolean).

### `info`

- Type: `string`
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/menu-items-choice/index.tsx
Expand Up @@ -58,6 +58,7 @@ function MenuItemsChoice( {
<MenuItem
key={ item.value }
role="menuitemradio"
disabled={ item.disabled }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that this adds another possible prop to the allowed choices, this likely needs to be updated in the component README here:

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Included a mention in menu-item/README.md: 7cddb17

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another side comment - I think this change is worth getting a components package CHANGELOG entry.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a changelog entry 6e2744c

icon={ isSelected && check }
info={ item.info }
isSelected={ isSelected }
Expand Down
5 changes: 5 additions & 0 deletions packages/components/src/menu-items-choice/types.ts
Expand Up @@ -2,6 +2,7 @@
* Internal dependencies
*/
import type { ShortcutProps } from '../shortcut/types';
import type { ButtonAsButtonProps } from '../button/types';

export type MenuItemsChoiceProps = {
/**
Expand Down Expand Up @@ -38,6 +39,10 @@ export type MenuItemChoice = {
* Unique value for choice.
*/
value: string;
/**
* Whether the menu item is disabled.
*/
disabled?: ButtonAsButtonProps[ 'disabled' ];
/**
* Additional information which will be rendered below the given label.
*/
Expand Down
27 changes: 23 additions & 4 deletions packages/edit-post/src/components/header/mode-switcher/index.js
Expand Up @@ -55,12 +55,31 @@ function ModeSwitcher() {
return null;
}

if ( ! isRichEditingEnabled || ! isCodeEditingEnabled ) {
return null;
let selectedMode = mode;
if ( ! isRichEditingEnabled && mode === 'visual' ) {
selectedMode = 'text';
}
if ( ! isCodeEditingEnabled && mode === 'text' ) {
selectedMode = 'visual';
}

const choices = MODES.map( ( choice ) => {
if ( choice.value !== mode ) {
if ( ! isCodeEditingEnabled && choice.value === 'text' ) {
choice = {
...choice,
disabled: true,
};
}
if ( ! isRichEditingEnabled && choice.value === 'visual' ) {
choice = {
...choice,
disabled: true,
info: __(
'You can enable the visual editor in your profile settings.'
),
};
}
if ( choice.value !== selectedMode && ! choice.disabled ) {
return { ...choice, shortcut };
}
return choice;
Expand All @@ -70,7 +89,7 @@ function ModeSwitcher() {
<MenuGroup label={ __( 'Editor' ) }>
<MenuItemsChoice
choices={ choices }
value={ mode }
value={ selectedMode }
onSelect={ switchEditorMode }
/>
</MenuGroup>
Expand Down