Bokeh legend callback issue

I’m in the process of updating some code from Bokeh 1.4 to Bokeh 3.7. One part of the code uses CustomJS to control the position of a legend using a select widget. The options are things like “Top Left”, “Top Center”, etc., and after the user makes their selection, the legend automatically moves to the new location.

In Bokeh 3.7, the legend moves to the correct location, but it loses the line drawings. The line drawings come back if I click on the legend.

Here are some images to illustrate the issue.

I’m using Python 3.13 and Bokeh 3.7. Here is the code snippet defining the callback when someone makes a dropdown selection

callback = CustomJS(
            args={
                'pos_select': pos_select,
                'legend': legend
            },
            code="""
                var pos = pos_select.value.toLowerCase().replace(' ', '_');
                if(pos === 'hide_legend'){
                    legend.visible = false;
                } else {
                    legend.location = pos;
                    legend.visible = true;
                }
                legend.change.emit();

                // Need to reapply the bokeh styles after making changes.
                applyBokehStyles();
            """
        )
pos_select.js_on_change('value', callback)

I have no idea what needs to be changed for this to work in Bokeh 3.7. Thanks for any help.

It’s possible there’s a bug/regression or maybe there’s just some usage change. I’m happy to take a quick closer look but I’d need a complete Minimal Reproducible Example that I can just drop in an editor and run directly without changes to investigate.