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

Error with documentation regarding how to use renderPlaceholder #5782

Open
ianleeduckworth opened this issue Dec 20, 2024 · 0 comments · May be fixed by #5783
Open

Error with documentation regarding how to use renderPlaceholder #5782

ianleeduckworth opened this issue Dec 20, 2024 · 0 comments · May be fixed by #5783

Comments

@ianleeduckworth
Copy link

Problem
The documentation around renderPlaceholder appears to be wrong, specifically the example usage. I used the prescribed example exactly and the placeholder behavior seemed incorrect. I noticed that it is because the attributes object that comes in has a style tag that must be applied to the div element. As such I believe the right way to use this functionality is to extend style instead of overriding it

Solution
This is the recommended approach to make a custom placeholder per the docs referenced below:

<Editable
  renderPlaceholder={({ attributes, children }) => (
    <div {...attributes} style={{ fontStyle: 'italic', color: 'gray' }}>
      {children}
    </div>
  )}
/>

However this appears not to work because attributes that comes in has styles already attached to it. If these styles are overridden as in the example above, you get some undesirable behavior and some errors. I got it to work by modifying the example slightly as such:

<Editable
  placeholder="Enter text here..."
  renderPlaceholder={({ attributes, children }) => {
    const styledAttributes = {
      ...attributes,
      style: {
        ...attributes.style,
        color: controlPlaceholderColor,
        opacity: 1,
      },
    };
    return <div {...styledAttributes}>{children}</div>;
  }}
</Editable>

The above example does two things:

  1. It respects the existing styles and only extends; this means that as long as you don't override critical properties like the position etc it will still work fine
  2. It actually adds a placeholder which is what will be rendered as children in the renderPlaceholder function

Alternatives
I can't really see any alternatives; the functionality works fine. If I am correct it's just the docs that need to be updated

Context
Documentation I'm referencing: https://docs.slatejs.org/libraries/slate-react/editable.

@ianleeduckworth ianleeduckworth linked a pull request Dec 22, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant