Issue #2350✓ SolvedOpened October 23, 2019by AbdiasM14 reactions

How do I add elements/sectors to views container using a custom button in the Panel

快速解答by pouyamiralayi13

@AbdiasM @ko06 sorry for being late on this! based on #277 Cheers!

Read full answer below ↓

Question

代码片段TEXT
I have added a custom button to the Panel as below:

        const pm = editor.Panels;        
        pm.addButton('views', {
            id: 'customButton',
            className: '',
            command: {
                run: function (editor) {
                    alert("Hello");
                },
                stop: function (editor) {

                }
            },
            attributes: { class: 'fa fa-address-card-o' },
            active: false,
        });

I would like to add custom elements in the views-container when this button is clicked. Screen shot attached. Could you please help with this?

![CustomButtonClick](https://user-images.githubusercontent.com/18558443/67381298-540e1d00-f5a9-11e9-8c79-4d3284820d32.PNG)

Answers (3)

👍 Most helpfulpouyamiralayiFebruary 18, 2020

@AbdiasM @ko06 sorry for being late on this! based on #277

        const pn = editor.Panels
        let editPanel = null
        pn.addButton('views', {
            id: 'editMenu',
            attributes: {class: 'fa fa-address-card-o', title: "Edit Menu"},
            active: false,
            command: {
                run: function (editor) {
                    if(editPanel == null){
                        const editMenuDiv = document.createElement('div')
                        editMenuDiv.innerHTML = `
                        <div id="your-content">
                             Input: <input/>
                             <button>Button</button> 
                              <!-- eg. bind a click event on button and do something with GrapesJS API -->
                        </div>
                    `
                        const panels = pn.getPanel('views-container')
                        panels.set('appendContent', editMenuDiv).trigger('change:appendContent')
                        editPanel = editMenuDiv
                    }
                    editPanel.style.display = 'block'
                },
                stop: function (editor) {
                    if(editPanel != null){
                        editPanel.style.display = 'none'
                    }
                }

            }
        })

Cheers!

hc0127December 12, 2023

Hi, @pouyamiralayi . thanks for your post. I got custom view container. But I wanna use react components on that part. Is it possible? if so, Please help me.

ko06October 24, 2019

is this fixed?

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Free option

Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.

Browse free plugins →
Premium option

Premium plugins ship with support, regular updates, and production-ready features — save days of integration work.

Browse premium plugins →

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.