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

RichTextToolbarButton adding dashicons makes them squashed to text #29376

Closed
dingo-d opened this issue Feb 26, 2021 · 3 comments · Fixed by #43574
Closed

RichTextToolbarButton adding dashicons makes them squashed to text #29376

dingo-d opened this issue Feb 26, 2021 · 3 comments · Fixed by #43574
Assignees
Labels
[Feature] Rich Text Related to the Rich Text component that allows developers to render a contenteditable [Package] Rich text /packages/rich-text [Status] In Progress Tracking issues with work in progress

Comments

@dingo-d
Copy link
Member

dingo-d commented Feb 26, 2021

Description

When creating a custom formatting button for the RichText component using RichTextToolbarButton, if you specify the icon as the name of the dashicons icon

	return <RichTextToolbarButton
		icon='editor-indent'
		title={ __('Toggle indent') }
		onClick={() => setAttributes({[`${componentName}IsIndented`]: !isIndented})}
		isActive={ isIndented }
	/>

you get this

Screenshot 2021-02-26 at 13 59 28

However, if you use svg - and not just copy pasted one, that won't work, you need to recreate the svg using primitives

	import { SVG, Path, Rect, G } from '@wordpress/primitives';

	const indent = (
		<SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
			<Rect x="0" fill="none" width="20" height="20"/>
			<G>
				<Path d="M3 5V3h9v2H3zm10-1V3h4v1h-4zm0 3h2V5l4 3.5-4 3.5v-2h-2V7zM3 8V6h9v2H3zm2 3V9h7v2H5zm-2 3v-2h9v2H3zm10 0v-1h4v1h-4zm-4 3v-2h3v2H9z"/>
			</G>
		</SVG>
	);

	return <RichTextToolbarButton
		icon={indent}
		title={ __('Toggle indent') }
		onClick={() => setAttributes({[`${componentName}IsIndented`]: !isIndented})}
		isActive={ isIndented }
	/>

It looks ok because the SVG element will be rendered instead of dashicons which is just added as a pseudo-element and has no styling for it.

Screenshot 2021-02-26 at 14 02 35

SVG elements have

.components-button.has-icon.has-text svg {
    margin-right: 8px;
}

whereas dashicons icons have

.components-button.has-icon .dashicon {
    display: inline-block;
    flex: 0 0 auto;
}

Notice the missing margin on the right.

Step-by-step reproduction instructions

Expected behaviour

Stylings should be the same

Actual behaviour

Stylings are not the same, and I need to copy and paste, then transform the SVG to 'primitives' in order to be rendered properly.

WordPress information

  • WordPress version: 5.6.2
  • Gutenberg version: Core
  • Are all plugins except Gutenberg deactivated? No, but it's a core bug, not a plugin bug.
  • Are you using a default theme (e.g. Twenty Twenty-One)? No, and it shouldn't matter, this is a part of core GB functionality

Device information

  • Device: Desktop
  • Operating system: MacOs Big Sur 11.2.1
  • Browser: Chrome Version 88.0.4324.192 (Official Build) (x86_64)
@Quintis1212
Copy link
Contributor

Quintis1212 commented Feb 26, 2021

Hello @dingo-d ) You can paste your svg as native svg tag with low letters :
Path gutenberg/packages/format-library/src/bold/index.js
`

          import { __ } from '@wordpress/i18n';
          import { toggleFormat } from '@wordpress/rich-text';
          import {
              RichTextToolbarButton,
              RichTextShortcut,
              __unstableRichTextInputEvent,
          } from '@wordpress/block-editor';
          import { formatBold } from '@wordpress/icons';
          
          const name = 'core/bold';
          const title = __( 'Bold' );

        export const bold = {
            name,
            title,
            tagName: 'strong',
            className: null,
            edit( { isActive, value, onChange, onFocus } ) {
	            function onToggle() {
		            onChange( toggleFormat( value, { type: name } ) );
	            }

	function onClick() {
		onToggle();
		onFocus();
	}

	const indent = (
		<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
				<path d="M3 5V3h9v2H3zm10-1V3h4v1h-4zm0 3h2V5l4 3.5-4 3.5v-2h-2V7zM3 8V6h9v2H3zm2 3V9h7v2H5zm-2 3v-2h9v2H3zm10 0v-1h4v1h-4zm-4 3v-2h3v2H9z"/>
		</svg>
	);

	return (
		<>
			<RichTextShortcut
				type="primary"
				character="b"
				onUse={ onToggle }
			/>
			<RichTextToolbarButton
				name="bold"
				icon={ indent }
				title={ title }
				onClick={ onClick }
				isActive={ isActive }
				shortcutType="primary"
				shortcutCharacter="b"
			/>
			<__unstableRichTextInputEvent
				inputType="formatBold"
				onInput={ onToggle }
			/>
		</>
	);
},

};
Снимок экрана от 2021-02-26 20-15-31

`

@dingo-d
Copy link
Member Author

dingo-d commented Feb 27, 2021

Hmmm didn't tested that out 🤔 When I just added the SVG as a string in the icon prop, it didn't render, but it tried to load dashicon 🤷🏼‍♂️

Thanks for the tip 👍🏼

@Quintis1212
Copy link
Contributor

You are welcome! Just remember that If you start a tag name from capital letter react-dom will be try to find this component , and if there is no such component it will throw error

@annezazu annezazu added [Feature] Rich Text Related to the Rich Text component that allows developers to render a contenteditable [Package] Rich text /packages/rich-text Needs Technical Feedback Needs testing from a developer perspective. labels Mar 3, 2021
@t-hamano t-hamano self-assigned this Aug 24, 2022
@t-hamano t-hamano removed the Needs Technical Feedback Needs testing from a developer perspective. label Aug 24, 2022
@github-actions github-actions bot added the [Status] In Progress Tracking issues with work in progress label Aug 24, 2022
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 [Package] Rich text /packages/rich-text [Status] In Progress Tracking issues with work in progress
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants