GithubHelp home page GithubHelp logo

Support block support class names and inline styles on the block wrapper and make `blockProps` optional about block-interactivity-experiments HOT 3 CLOSED

wordpress avatar wordpress commented on June 14, 2024
Support block support class names and inline styles on the block wrapper and make `blockProps` optional

from block-interactivity-experiments.

Comments (3)

luisherranz avatar luisherranz commented on June 14, 2024 1

Thanks, Bernie 🙂

For reference, the difference between the two attempts was:

  • First attempt: We picked the class and style attributes from the DOM during the hydration phase.
  • Second attempt: We serialized the block supports output (class and style) dynamically in PHP using a data- attribute.

The component code didn't change, so this DX still applies. We haven't made blockProps optional yet, though.

from block-interactivity-experiments.

luisherranz avatar luisherranz commented on June 14, 2024

So the solution here could be to pass a blockProps (or use the useBlockProps hook), but make it optional, only for the case where either the class name or the inline style of the block wrapper is going to be dynamically modified in the frontend.

A block that modifies the class name:

const View = ({ blockProps: { className } }) => {
  const [showBlock, setShowBlock] = useState(true);

  return (
    <div className={`${className} ${ showBlock ? "show" : "hide" }`}>
      ...
    </div>
  )
}

A block that modifies the inline style:

const View = ({ blockProps: { style } }) => {
  const [width, setWidth] = useState("100px");

  return (
    <div style={{ ...style, width }}>
      ...
    </div>
  )
}

A block that doesn't modify the class name nor the inline style:

const View = () => {
  return (
    <div>
      ...
    </div>
  )
}

Whether to use a blockProps prop or stick to the useBlockProps hook is something we could discuss later in the Gutenberg repo if we finally propose this.

from block-interactivity-experiments.

ockham avatar ockham commented on June 14, 2024

@luisherranz and I had a pair-programming session earlier today, where we first tried to implement Luis' idea:

diff --git a/src/blocks/block-hydration-experiments-parent/frontend/index.js b/src/blocks/block-hydration-experiments-parent/frontend/index.js
index 1ab5647..5871eb5 100644
--- a/src/blocks/block-hydration-experiments-parent/frontend/index.js
+++ b/src/blocks/block-hydration-experiments-parent/frontend/index.js
@@ -3,13 +3,13 @@ import Button from '../shared/button';
 import Title from '../shared/title';
 
 const Frontend = (
-       { blockProps, attributes: { counter: initialCounter, message }, children },
+       { blockProps: { className }, attributes: { counter: initialCounter, message }, children },
 ) => {
        const [ show, setShow ] = useState( false );
        const [ counter, setCounter ] = useState( initialCounter );
 
        return (
-               <div {...blockProps}>
+               <div className={`${className} ${ show ? "show" : "hide" }`}>
                        <Title message={message} />
                        <Button handler={() => setShow( !show )} />
                        <button onClick={() => setCounter( counter + 1 )}>{counter}</button>

However, this did not work: It would serialize the default value for the wrapping div's className (hide) and keep it around in the hydrated block, even when toggling show state. In other words, the className would always contain hide, even when showing the child block 🙃

block-props-hydration

So instead, we tried a different approach: #41

from block-interactivity-experiments.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.