Selectors not working in JIVE getting started example

I am attempting to follow the “Getting started” example listed in the JIVE repository, and I’m running into some issues with using the selectors. Neither selecting the “greeting” message nor the button seems to work for changing their appearance.

 static juce::ValueTree topLevel(const juce::String &greeting)
{
    static constexpr auto style = [] {
        return new jive::Object{
            {"background", "#14181D"},
            {"foreground", "#CDD9E5"},
            {"font-family", "Verdana"},
            {"font-size", 15},
            // GREETING SELECTOR HERE
            {
                "#greeting",
                new jive::Object{
                    {"font-size", 25},
                },
            },
            // BUTTON SELECTOR HERE
            {
                "Button",
                new jive::Object{
                    {"background", "#255AB2"},
                    {"border-radius", 10},
                    {
                        "hover",
                        new jive::Object{
                            {"background", "#316DCA"},
                        },
                    },
                },
            },
        };
    };

    return juce::ValueTree{
        "Window",
        {
            {"style", style()},
            {"width", 640},
            {"height", 400},
            {"justify-content", "centre"},            
            {"align-items", "centre"},
        },
        {
            juce::ValueTree{
                "Text",
                {
                    {"id", "greeting"},
                    {"text", greeting},
                    {"margin", "0 0 20 0"},
                },
            },
        },
    };
}
// ------------------------ //
// inside of initialise //
        juce::Timer::callAfterDelay(2000, [this]() mutable {
            view.appendChild(juce::ValueTree{
                "Button",
                {
                    {"padding", "7 20"},
                },
                {
                    juce::ValueTree {
                        "Text",
                        {
                            { "text", "Click me!" },
                        },
                    },
                },
            }, nullptr);
        });

All of this code is actually inside of Main.cpp right now. From comparing my code to the example, I don’t see anything different between the two. Is there something I’m missing here, or is there some bug with the code currently?

Ahh yes, this is something I’ve been meaning to fix but it’s a bit knarly.

For now, you’ll just have to specify the styles for each specific component. I.e. move the #greeting styles to their own object and set them on the greeting text item itself.

1 Like

That works! Thanks for the heads-up.

What’s the best way for stylesheet-like functionality for now? I’m wondering if there’s a way to put the separate stylesheets in a map or some data structure that can be “globally” accessed inside of a class or file.

I’d recommend writing functions that just return elements with their own style properties.

E.g. instead of defining a Button selector in your top-level stylesheet, instead write a function that returns a button element:

[[nodiscard]] static auto button(...)
{
    return juce::ValueTree {
        "Button",
        {
            {
                "style",
                new jive::Object {
                    //...
                },
            },
        },
    };
}

Then you can just call button() as many times as you like, and pass in arguments to customise its properties like colour, name, etc.

This is the same basic idea when using React with something like styled-components for styling.

1 Like