Make the element IDs copy to clipboard on click

At times I have to use their IDs and am left with typing them instead by seeing and the Bricks ID structure is not a convenient one to remember so why not allow copying the ID on clicking a key that is maybe next to the ‘Rename ID’ pencil icon?

I am surprised even in the rename field, the existing ID doesn’t get auto typed to edit/copy.

Issue

9 Likes

+1 for both class and id copy to clipboard :slight_smile:

4 Likes

What do you think about this, man:

Also +1 for that.

@timmse what do you think about it, any chance that we can get a static ID text field somewhere above the word “container” or anywhere else to copy and paste the ID?

I’ve also mentioned in facebook that there is a UX problem, when giving an Element a custom ID via Advanced->CSS->ID there’s no chance that we can find out the ID of the element without removing that ID!

TIA for your answer! :wink:

Would really like to see this as well. Being able to copy a class name is pretty essential when using BEM to name your classes :slight_smile:

1 Like

Fully second this … have also been missing a copy-id function heavily!

1 Like

I would very much like to see this added. Or at least a small copy icon. I understand that clicking is already tied to opening the global class menu.

2 Likes

For those interested in a DIY method while we wait, I threw something together today. If you find any issues, let me know.

Add this to your functions.php file:

add_action( 'wp_enqueue_scripts', function() {
	if ( bricks_is_builder_main() ) {
		wp_enqueue_script( 'stu-element-copy-id', get_stylesheet_directory_uri() . '/stu-element-copy-id.js');
	}
} );

And put this in a file called stu-element-copy-id.js inside your child theme:

function stuElementCopyId() {
	const copyIcon = '<svg version="1.1" viewBox="0 0 30 30" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" class="bricks-svg"><path d="M9.5,5h11c0.276,0 0.5,-0.224 0.5,-0.5v-1c0,-1.378 -1.122,-2.5 -2.5,-2.5h-1.774c-0.353,-0.609 -1.007,-1 -1.726,-1c-0.718,0 -1.373,0.39 -1.726,1h-1.774c-1.378,0 -2.5,1.122 -2.5,2.5v1c0,0.276 0.224,0.5 0.5,0.5Zm14.766,-2h-1.766c-0.276,0 -0.5,0.224 -0.5,0.5v1c0,0.828 -0.672,1.5 -1.5,1.5h-11c-0.826,0 -1.5,-0.672 -1.5,-1.5v-1c0,-0.276 -0.224,-0.5 -0.5,-0.5h-1.765c-0.956,0 -1.735,0.781 -1.735,1.739v23.523c0,0.957 0.779,1.738 1.735,1.738h18.529c0.957,0 1.736,-0.781 1.736,-1.739v-23.522c0,-0.958 -0.778,-1.739 -1.734,-1.739Z" fill="currentColor"></path></svg>';

	const span = document.createElement('span');
	span.setAttribute('class', 'bricks-svg-wrapper stu-copy');
	span.setAttribute('data-balloon', 'Copy (Element ID)');
	span.setAttribute('data-balloon-pos', 'top-right');
	span.innerHTML = copyIcon;

	const style = document.createElement('style');
	style.innerHTML = `
		#bricks-panel-element-classes .active-class .actions .stu-copy {
			visibility: hidden;
		}
		#bricks-panel-element-classes .active-class:hover .actions .stu-copy {
			visibility: visible;
		}
	`;
	document.head.appendChild(style);

	const targetNode = document.getElementById('bricks-panel'),
		  config = {childList: true, subtree: true},
		  callback = (mutationList, observer) => {
			  for (const mutation of mutationList) {

				      // If changing element being edited
				  if (mutation.target.id == 'bricks-panel-element' ||
					  // If going from settings panel or element picker to editing element
					  mutation.addedNodes.length !== 0 && mutation.addedNodes[0].id === 'bricks-panel-element') {

					  const activeClass = document.querySelector('#bricks-panel-element-classes .active-class'),
							placeholder = activeClass.querySelector('input').getAttribute('placeholder'),
							dataId = document.getElementById('bricks-builder-iframe').contentWindow.document.querySelector(placeholder).getAttribute('data-id'),
							actionsContainer = activeClass.querySelector('div.actions');

					  actionsContainer.prepend(span);

					  const copy = actionsContainer.querySelector('span.stu-copy');

					  copy.addEventListener('click', function(e) {
						  e.stopPropagation();

						  // Comment this line and uncomment the next line if you'd  prefer getting the bricks set element id always
						  navigator.clipboard.writeText(placeholder);

						  // Uncomment this line if you want the bricks set element id
						  // navigator.clipboard.writeText(dataId);
					  });

				  }
			  }
		  };

	const observer = new MutationObserver(callback);

	observer.observe(targetNode, config);
}

document.addEventListener('DOMContentLoaded', function() {
	stuElementCopyId();
});
9 Likes

Thanks, i hope it will be added built-in in bricks builder
It is very helpful for custom css

1 Like

+1 really nice UX

please implement this soon

1 Like

Beautiful snippet. Thank you.

1 Like

Hey all,

There’s been a few bugs reported lately. Mainly it doesn’t work with custom ID’s. I can’t update my original reply because too much time has passed. I’ve included a link to a GitHub project that has a fix. I’ll include future fixes there as well:

4 Likes

Much thanks, mate :slight_smile: