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

add RichTextCharacterLimit component #133

Merged
merged 4 commits into from
Jul 22, 2022
Merged

add RichTextCharacterLimit component #133

merged 4 commits into from
Jul 22, 2022

Conversation

darrenjacoby
Copy link
Contributor

@darrenjacoby darrenjacoby commented Jul 22, 2022

Description of the Change

Extend RichText with RichTextCharacterLimit in order to enable character limits on rich text content.

How to test the Change

import { RichTextCharacterLimit } from '@10up/block-components';

function BlockEdit(props) {
    const { attributes, setAttributes } = props;
    const { title } = attributes;

    return (
        <RichTextCharacterLimit
            limit={30}
            enforce={true}
            tagName='h2'
            value={title}
            onChange={(title) => setAttributes({title})}
            allowedFormats={[
                'core/bold',
                'core/link'
            ]}
        />
    )
}

Attribute title should never exceed the character limit unless enforce is set to false. If enforce is set to false, the author will be able to exceed the character limit. Both instances will show a character counter below the RichText component.

Credits

Props @fabiankaegy

Related

#131
@fabiankaegy should we favour a component versus a hook? The implementation and DX feels cleaner.

Checklist:

  • I agree to follow this project's Code of Conduct.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests pass.

Please let me know if I can add tests.

@darrenjacoby darrenjacoby changed the title Rich text character limit component RichTextCharacterLimit component Jul 22, 2022
@darrenjacoby darrenjacoby changed the title RichTextCharacterLimit component add RichTextCharacterLimit component Jul 22, 2022
@darrenjacoby
Copy link
Contributor Author

I wasn't sure if some default styling for <Counter /> is preferable, but let me know and I can add some.

@fabiankaegy
Copy link
Member

@darrenjacoby Yeah honestly that is the main thing I am currently still thinking about. Ideally, the element should be absolutely positioned/floating in the corner of the RichText component. And only visible if the block is currently selected.

I'm wondering whether we can apply some magic js based positioning to it since I don't want to introduce a wrapper element that would change the editors markup compared to the frontend.

@fabiankaegy
Copy link
Member

Maybe we can use https://floating-ui.com/ to get the element so float in the corner when the block is selected.

We can get the selection state info via the useBlockEditContext hook

const { clientId } = useBlockEditContext();

@darrenjacoby
Copy link
Contributor Author

Sounds good, I'll see what we can do based on that recommendation!

@fabiankaegy fabiankaegy changed the base branch from develop to feature/add-character-limit-rich-text-component July 22, 2022 11:01
@fabiankaegy
Copy link
Member

I'm going to merge this onto a new branch in this repo so I can also add to it from there 👍

@fabiankaegy fabiankaegy merged commit 0a9a976 into 10up:feature/add-character-limit-rich-text-component Jul 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants