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

RichText don't work properly in InspectorControls because BlockEditContextProvider is not providing the right context. #9203

Closed
hos-shams opened this issue Aug 21, 2018 · 7 comments · Fixed by #16807
Labels
[Feature] Rich Text Related to the Rich Text component that allows developers to render a contenteditable [Type] Bug An existing feature does not function as intended

Comments

@hos-shams
Copy link

I want to display a RichText component in a block's panel. The RichText displays just fine, but there's not any toolbar to add a link to text or make it bold. Could you please take a look at this bug or provide a solution to overcome this issue?

const { Fragment } = wp.element;
const { PanelBody, Modal, ColorPalette } = wp.components;
const { InspectorControls, RichText } = wp.editor;
const { __ } = wp.i18n;
const { registerBlockType } = wp.blocks;

registerBlockType( 'shamsoft/issues', {
  title: __( 'Issues' ),
  icon: 'universal-access-alt',
  category: 'layout',
  attributes: {
    content: {
      type: 'array',
      default: [],
    }
  },

  edit( props ) {

    return [
      <Fragment>
        <InspectorControls>
          <PanelBody title={ __( 'Panel' ) }>
            <RichText
              tagName="p"
              value={ props.attributes.content }
              onChange={ (v) => props.setAttributes( { content: v } ) }
              placeholder={ __( 'Add text or type / to add content' ) }
            />
          </PanelBody>
        </InspectorControls>

        <div>None</div>
      </Fragment>
    ];
  },

  save() {
    return <div>None</div>;
  },
} );
@designsimply designsimply added [Type] Help Request Help with setup, implementation, or "How do I?" questions. Needs Technical Feedback Needs testing from a developer perspective. [Feature] Rich Text Related to the Rich Text component that allows developers to render a contenteditable labels Aug 21, 2018
@DannyCooper
Copy link
Contributor

You need to use the Block Controls component:

https://wordpress.org/gutenberg/handbook/blocks/block-controls-toolbars-and-inspector/

@youknowriad
Copy link
Contributor

Does the toolbar show up in the block's toolbar instead?

The RichText component was never meant to be used this way (in the Inspector) but I assume you can use inlineToolbar={ true } prop to show the toolbar above the RichText component.

@youknowriad
Copy link
Contributor

I'm closing, let me know if it works for you. Thanks.

@hos-shams
Copy link
Author

@youknowriad No, the toolbar doesn't even show up in the block's toolbar. Also, inlineToolbar={ true } doesn't display the toolbar.

While you didn't meant to support RichText in the Inspector, but it would be nice to have this functionality. Sometimes we need to create content for a hidden container, such as a modal. For example, there's a button in the block which triggers a modal onClick. We, as a theme developer, should provide a RichText in the Inspector for the user, so it can write his modal's content.

@youknowriad youknowriad reopened this Aug 23, 2018
@youknowriad
Copy link
Contributor

Ok, So I took a look at this and indeed it's not working and it's more "complex" than I though. The issue is that the RichText component always received isSelected=false even if the block is selected. And it happens probably because the Inspector controls is rendered in a Slot that is not wrapped in the correct BlockEditProvider. Not sure how to fix this at the moment. I'm reopening the issue so we can track it.

@youknowriad youknowriad added [Type] Bug An existing feature does not function as intended and removed Needs Technical Feedback Needs testing from a developer perspective. [Type] Help Request Help with setup, implementation, or "How do I?" questions. labels Aug 23, 2018
@youknowriad youknowriad changed the title RichText in a block panel doesn't display the toolbar RichText don't work properly in InspectorControls because BlockEditContextProvider is not providing the right context. Aug 23, 2018
@aduth
Copy link
Member

aduth commented May 31, 2019

One possibility is to use the bubblesVirtually variant of Slot/Fill, which uses React's createPortal API and would respect context automatically (vs. the default implementation, which will lose context).

https://github.com/WordPress/gutenberg/tree/master/packages/components/src/slot-fill
https://reactjs.org/docs/portals.html

Last time this was tried, there was some unintended consequences around how events bubble in bubblesVirtually which the application is not yet prepared to handle (#3082, #3083).

@giannis-koulouris-bb
Copy link

Do we have any updates on this? It looks as closed but I'm not sure if it's supposed to be resolved. I had a case where I wanted to render RichText component on Inspector Controls and the toolbar is not visible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Rich Text Related to the Rich Text component that allows developers to render a contenteditable [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants