acf-composer icon indicating copy to clipboard operation
acf-composer copied to clipboard

Docs: add note about Block Supports

Open mike-sheppard opened this issue 3 years ago • 7 comments

Note: popping this here until I have time to submit a PR (or someone else beats me to it 🙏 )

Didn't know this until reading it in this PR but ACF Blocks supports all native Block Supports including color, typography, spacing etc

...
    public $supports = [
        'align'         => true,
        'jsx'           => true,
        ...
        'color'         => [
            'text'       => true,
            'background' => true,
        ],
        'spacing'       => [
            'margin'  => true,
            'padding' => true,
        ],
    ];
...

2022-02-02 1552 - Google Chrome

2022-02-02 1554 - Google Chrome

mike-sheppard avatar Feb 16 '22 16:02 mike-sheppard

nice!

Log1x avatar Feb 16 '22 21:02 Log1x

Thanks for this! Been searching everywhere to see if it was possible.

squareone-jarrod avatar Mar 10 '22 11:03 squareone-jarrod

After adding color support to your block.php file, how do you pass that into {{ $block->classes }}? I'm not seeing "has-primary-color" on the front end, though I do see it get added to the block when I inspect the editor.

colinswinney avatar Mar 25 '22 04:03 colinswinney

hey @colinswinney it isn't currently handled by ACF Composer or ACF for that matter so you'll need to construct the class names with the slugs returned in $block->block->backgroundColor & $block->block->textColor

mike-sheppard avatar Mar 25 '22 11:03 mike-sheppard

a very quick and dirty example to get you started, you'll need to check they exist etc

<div class="has-{{ $block->block->backgroundColor }}-background-color has-background">

mike-sheppard avatar Mar 25 '22 11:03 mike-sheppard

Fantastic, thanks @mike-sheppard! I appreciate it!

colinswinney avatar Mar 25 '22 12:03 colinswinney

If it helps anyone, here's a method I've been using to prepare the text and background color classes for the block's view.

in the block's class file:

/**
 * Assembles the block's text and background color classes
 *
 * @return string
 */
public function getColorClasses()
{
    // No need to generate them if we're inside the Editor:
    if ($this->preview) {
        return '';
    }

    $classes = [];

    $bgColor = $this->block->backgroundColor ?? null;
    if (!empty($bgColor)) {
        $classes[] = sprintf('has-background has-%s-background-color', $bgColor);
    }

    $textColor = $this->block->textColor ?? null;
    if (!empty($textColor)) {
        $classes[] = sprintf('has-%s-color', $textColor);
    }

    return implode(' ', $classes);
}

send it to the view via the with() method:

public function with()
{
    return [
        /* ... */
        'colorClasses' => $this->getColorClasses(),
    ];
}

and in the accompanying blade template, via the new-ish @class directive, we can simply use:

<div @class([$block->classes, $colorClasses])>

It would be neater just to append to $block->classes, but not sure how that could be done... or better yet, if ACF would just do it for us :) HTH!

daverobertson avatar Apr 05 '22 17:04 daverobertson

If anyone wants to do a PR adding these to the block stub $supports as well as using some of the logic from @daverobertson's post above to add the classes to $block->classes as needed it'd be super appreciated. ❤️

Log1x avatar Oct 14 '22 18:10 Log1x

#145

Log1x avatar Aug 01 '23 09:08 Log1x