Issue #2461Opened December 20, 2019by obrazkow2 reactions

How to apply style or trait to parent or child element of selected component

Question

For example i have component image that wrapped in div.

Code SnippetTEXT
I want to style image with multiple properties(horizontal position, border-radius, etc)
When update horizontal position, need to update current element - div
When update border-radius, need to update child element - img.
Also i have such problem with Trait, i cannot update src for img.
I added listener at Trait and on change i apply value on img, then div and img had src together, but it's bad solution.
`
export default (editor) => {
    editor.TraitManager.addType('image', {
        onEvent({ elInput, component, event }) {
            component.view.el.querySelector('img').src = elInput.value
        },
    });
}
`
 Can you advice me solution ?

Answers (3)

pouyamiralayiDecember 21, 20191 reactions

@obrazkow You can access the parent like this: component.parent(); and for the children: const collection = component.components();

Also i have such problem with Trait, i cannot update src for img.

 editor.DomComponents.addType('image', {
            model: {
                defaults: {
                    traits: ['src']
                }
            }
        })

cheers!

pouyamiralayiDecember 21, 20191 reactions

With your solution trait don't know src value and input will be empty at init editor phase.

i do not understand what you are talking about!

What do you think about my solution with override target in init method

you are treating a very simple use case with an advanced solution, and i do not personally recommend defining custom trait for a case like this. cheers!

obrazkowDecember 21, 20190 reactions

@pouyamiralayi that's exactly what I did, but with such solution selected component have attribute or style that need only for child component.

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...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.