-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Default appender: try editable paragraph instead of text area #30986
Changes from all commits
3955b1c
8695803
9d8e777
bf37fa3
02a5da3
36e8678
e416513
4f7fe2a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,3 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import TextareaAutosize from 'react-autosize-textarea'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
|
@@ -18,6 +13,12 @@ import { withSelect, withDispatch } from '@wordpress/data'; | |
import Inserter from '../inserter'; | ||
import { store as blockEditorStore } from '../../store'; | ||
|
||
/** | ||
* Zero width non-breaking space, used as padding for the paragraph when it is | ||
* empty. | ||
*/ | ||
export const ZWNBSP = '\ufeff'; | ||
|
||
export function DefaultBlockAppender( { | ||
isLocked, | ||
isVisible, | ||
|
@@ -33,34 +34,30 @@ export function DefaultBlockAppender( { | |
const value = | ||
decodeEntities( placeholder ) || __( 'Type / to choose a block' ); | ||
|
||
// The appender "button" is in-fact a text field so as to support | ||
// transitions by WritingFlow occurring by arrow key press. WritingFlow | ||
// only supports tab transitions into text fields and to the block focus | ||
// boundary. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Arrow key navigation into the appender works fine. Confirmed in Firefox too. |
||
// | ||
// See: https://github.com/WordPress/gutenberg/issues/4829#issuecomment-374213658 | ||
// | ||
// If it were ever to be made to be a proper `button` element, it is | ||
// important to note that `onFocus` alone would not be sufficient to | ||
// capture click events, notably in Firefox. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need to make it a proper |
||
// | ||
// See: https://gist.github.com/cvrebert/68659d0333a578d75372 | ||
|
||
// The wp-block className is important for editor styles. | ||
|
||
return ( | ||
<div | ||
data-root-client-id={ rootClientId || '' } | ||
className="wp-block block-editor-default-block-appender" | ||
className="block-editor-default-block-appender" | ||
> | ||
<TextareaAutosize | ||
<p | ||
tabIndex="0" | ||
// Only necessary for `useCanvasClickRedirect` to consider it | ||
// as a target. Ideally it should consider any tabbable target, | ||
// but the inserter is rendered in place while it should be | ||
// rendered in a popover, just like it does for an empty | ||
// paragraph block. | ||
contentEditable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, as commented above, we need move the inserter component below to a popover (just like in the empty paragraph) in order for this to work correctly without adding the content editable attribute. Will do that in a follow up. |
||
suppressContentEditableWarning | ||
// We want this element to be styled as a paragraph by themes. | ||
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-to-interactive-role | ||
role="button" | ||
aria-label={ __( 'Add block' ) } | ||
className="block-editor-default-block-appender__content" | ||
readOnly | ||
// The wp-block className is important for editor styles. | ||
className="wp-block block-editor-default-block-appender__content" | ||
onFocus={ onAppend } | ||
value={ showPrompt ? value : '' } | ||
/> | ||
> | ||
{ showPrompt ? value : ZWNBSP } | ||
</p> | ||
<Inserter | ||
rootClientId={ rootClientId } | ||
position="bottom right" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,34 +12,9 @@ | |
outline: 1px solid transparent; | ||
} | ||
|
||
textarea.block-editor-default-block-appender__content { // Needs specificity in order to override input field styles from WP-admin styles. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check out how much CSS we can remove due to this change. Very cool! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
font-family: inherit; | ||
font-size: inherit; | ||
border: none; | ||
background: none; | ||
box-shadow: none; | ||
display: block; | ||
cursor: text; | ||
width: 100%; | ||
outline: $border-width solid transparent; | ||
transition: 0.2s outline; | ||
@include reduce-motion("transition"); | ||
margin-top: $default-block-margin; | ||
margin-bottom: $default-block-margin; | ||
|
||
// This needs high specificity as it's output as an inline style by the library. | ||
resize: none !important; | ||
|
||
// Emulate the dimensions of a paragraph block. | ||
// On mobile and in nested contexts, the plus to add blocks shows up on the right. | ||
// The rightmost padding makes sure it doesn't overlap text. | ||
padding: 0 #{ $block-padding + $button-size } 0 0; | ||
|
||
// Use opacity to work in various editor styles. | ||
color: $dark-gray-placeholder; | ||
.is-dark-theme & { | ||
color: $light-gray-placeholder; | ||
} | ||
.block-editor-default-block-appender__content { | ||
// Set the opacity of the initial block appender to the same as placeholder text in an empty Paragraph block. | ||
opacity: 0.62; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: Is there a SASS variable for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, I checked and I think it's fine for now to not create a variable. The value is only used in one other place and there's a comment on the reason for this value. Maybe we should use a class instead of a variable? Cc @jasmussen In any case, we can iterate. |
||
} | ||
|
||
// Dropzone. | ||
|
@@ -48,11 +23,6 @@ | |
} | ||
} | ||
|
||
// Ensure that the height of the first appender, and the one between blocks, is the same as text. | ||
.block-editor-default-block-appender__content { | ||
line-height: $editor-line-height; | ||
} | ||
|
||
// Empty / default block side inserter. | ||
.block-editor-block-list__empty-block-inserter.block-editor-block-list__empty-block-inserter, // Empty paragraph, needs specificity to override inherited popover styles. | ||
.block-editor-default-block-appender .block-editor-inserter { // Empty appender. | ||
|
@@ -79,9 +49,3 @@ | |
display: none; | ||
} | ||
} | ||
|
||
.block-editor-default-block-appender .block-editor-inserter { | ||
@include break-small { | ||
align-items: center; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,10 +55,9 @@ | |
margin-left: auto; | ||
margin-right: auto; | ||
|
||
// Apply default block margin below the post title. | ||
// This ensures the first block on the page is in a good position. | ||
// This rule can be retired once the title becomes an actual block. | ||
margin-bottom: $default-block-margin; | ||
// Margins between the title and the first block, or appender, do not collapse. | ||
// By explicitly setting this to zero, we avoid "double margin". | ||
margin-bottom: 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What makes this different from a normal paragraph? Do we have this rule for the paragraph block too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this can be removed. I think at one point a "wp-block" class was attached to this as well. But it looks like it can be removed from my testing! |
||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For posterity, what benefits do we see from using a non-breaking space vs an empty string?
I 🔍 a little bit and
ZWNBSP
is also better known for its use as a byte order mark. We might also want to consider using word joiner\u2060
instead if we do need something other than an empty string for padding.From: https://www.unicode.org/faq/utf_bom.html#bom6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An empty string won't fill the element. We use the same character for rich text padding.