28th Apr '23
/
2 comments

Move Color Palette Selector To Top in Color Popup

In Bricks Facebook group a user asks:

In the colors popup in the builder, I’d really like to move the color palette selector from the bottom of the popup to the top – makes a lot more sense to me to have it there…

Update

As Maxime points out here, JS is not needed at all as we can simply use the CSS order property.

While the below JS method is not necessary for the task at hand, it could still be a handy reference for when something that’s possible only with MutationObserver is to be implemented.

This tutorial shows how we can use MutationObserver interface to watch for changes to the DOM tree for running JavaScript when a div having a data attribute of control set to color is added in the DOM i.e., when the color picker is opened in the Bricks editor.

Before

After

Step 1

Install and activate the Bricks child theme if it is not already active.

Create a directory named assets inside the child theme directory and inside that another directory named js.

Create a file named say builder.js inside the above directory having this code:

document.addEventListener("DOMContentLoaded", () => {
  // Select the node that will be observed for mutations
  const targetNode = document.querySelector("#bricks-panel")

  // Options for the observer (which mutations to observe)
  const config = { childList: true, subtree: true }
  // childList: Set to true to monitor the target node (and, if subtree is true, its descendants) for the addition of new child nodes or removal of existing child nodes. The default value is false.
  // subtree: Set to true to extend monitoring to the entire subtree of nodes rooted at target. All of the other properties are then extended to all of the nodes in the subtree instead of applying solely to the target node. The default value is false.

  // Callback function to execute when mutations are observed
  const callback = (mutationList, observer) => {
    for (const mutation of mutationList) {
      // console.log(mutation.target)
      if (
        mutation.target.hasAttribute("data-control") &&
        mutation.target.getAttribute("data-control") === "color"
      ) {
        const colorPaletteSelector = document.querySelector(
          ".color-palette-selector"
        )
        if (colorPaletteSelector) {
          const colorPaletteSelectorParent = colorPaletteSelector.parentElement
          colorPaletteSelectorParent.prepend(colorPaletteSelector)
        }
      }
    }
  }

  // Create an observer instance linked to the callback function
  const observer = new MutationObserver(callback)

  // Start observing the target node for configured mutations
  observer.observe(targetNode, config)

  // Create style tag and append to head
  document.head.appendChild(document.createElement("style")).innerHTML = `
    [data-control=color] .bricks-control-popup .color-modes {
        margin-bottom: 0;
    }
    
    [data-control=color] .bricks-control-popup .color-palette-selector {
        margin-bottom: 30px;
    }
    `
})

Step 2

Edit child theme’s functions.php.

Change

add_action( 'wp_enqueue_scripts', function() {
	// Enqueue your files on the canvas & frontend, not the builder panel. Otherwise custom CSS might affect builder)
	if ( ! bricks_is_builder_main() ) {
		wp_enqueue_style( 'bricks-child', get_stylesheet_uri(), ['bricks-frontend'], filemtime( get_stylesheet_directory() . '/style.css' ) );
	}
} );

to

add_action( 'wp_enqueue_scripts', function() {
	// Enqueue your files on the canvas & frontend, not the builder panel. Otherwise custom CSS might affect builder)
	if ( ! bricks_is_builder_main() ) {
		wp_enqueue_style( 'bricks-child', get_stylesheet_uri(), ['bricks-frontend'], filemtime( get_stylesheet_directory() . '/style.css' ) );
	} else {
		wp_enqueue_script( 'builder', get_stylesheet_directory_uri() . '/assets/js/builder.js', [], '1.0.0', true );
	}
} );

Thanks to cmstew and portseif for the JS code on which this is based.

Get access to all 610 Bricks code tutorials with BricksLabs Pro

2 comments

Leave your comment

 

Related Tutorials..

Pro
Opening Submenus with Click Instead of Hover in Bricks

Opening Submenus with Click Instead of Hover in Bricks

This Pro tutorial provides the code for opening submenus with click instead of hover in Bricks. Step 1 Add this in child theme's style.css: Step…
Categories:
Pro
WP Grid Builder Filters inside Bricks’ OffCanvas

WP Grid Builder Filters inside Bricks’ OffCanvas

This Pro tutorial provides the steps to move WP Grid Builder's filters inside the Bricks' native Offcanvas on smaller viewport widths. Let's work with the…
Pro
Button-specific HTML in Bricks Popup

Button-specific HTML in Bricks Popup

In the Bricks Facebook group a user asks: I need to create popups to expend the text (eg. read more). I wonder if I have…
Categories:
How to Change Bricks Preloader Background Color

How to Change Bricks Preloader Background Color

Working on a dark-themed site like our friend Storm and getting blinded by the light background of Bricks' preloader screen of the editor? Here's how…
Categories:
Opening RSS Block’s Links in New Tabs

Opening RSS Block’s Links in New Tabs

Looking to have links output by RSS Block (in the block editor) in new tab/window when using Bricks? Add this in your Bricks template/Page at…
Categories:
McDonald’s Order Promise Analogy

McDonald’s Order Promise Analogy

From the JavaScript course I am doing now: A Promise is like placing an order at a restaurant. You receive a ticket (Promise object) that…
Categories:
Pro
How to Link to a Specific Tab in Bricks

How to Link to a Specific Tab in Bricks

This Pro tutorial provides the JavaScript code that will automatically switch to the tab when using Nestable Tabs elements based on the "tab" URL parameter…
Categories:
Pro
Switching Tabs on Hover in Bricks

Switching Tabs on Hover in Bricks

This Pro tutorial shows how to make the tabs of Tabs (Nestable) elements active on hover in Bricks. Step 1 Add one or more Tabs…
Categories: