@@ -60,13 +60,26 @@ def instantiate_crossorigin_interface(key):
6060 width = 0 ,
6161 )
6262
63- @st .fragment
63+ class ForceAnchor :
64+ anchor :str
65+ def __init__ (self ):
66+ self .anchor = None
67+
68+ def push (self , anchor ):
69+ self .anchor = anchor
70+
71+ def pop (self ):
72+ anchor = self .anchor
73+ self .anchor = None
74+ return anchor
75+
76+ @st .fragment ()
6477def scroll_navbar (
6578 anchor_ids : Collection [str ],
6679 key : str = 'scroll_navbar_default' ,
6780 anchor_icons : Collection [str ] = None ,
6881 anchor_labels : Collection [str ] = None ,
69- force_anchor : str = None ,
82+ force_anchor : ForceAnchor | None = None ,
7083 orientation : Literal ['vertical' , 'horizontal' ] = 'vertical' ,
7184 override_styles : Dict [str , str ] = {},
7285 auto_update_anchor : bool = True ,
@@ -87,9 +100,9 @@ def scroll_navbar(
87100 A collection of labels for each navigation button.
88101 Each label corresponds to an anchor in anchor_ids.
89102 If None, the anchor IDs will be used. Defaults to None.
90- force_anchor (str, optional ):
91- An anchor ID to force navigation to.
92- Setting this will simulate clicking on an anchor. Defaults to None.
103+ force_anchor (str, ForceAnchor ):
104+ A ForceAnchor object to push anchors to programatically select .
105+ Setting this and pushing an anchor ID will simulate clicking on an anchor. Defaults to None.
93106 orientation (Literal['vertical', 'horizontal'], optional):
94107 The orientation of the navigation bar. Defaults to 'vertical'.
95108 override_styles (Dict[str, str], optional):
@@ -118,15 +131,17 @@ def scroll_navbar(
118131
119132 inject_crossorigin_interface ()
120133 instantiate_crossorigin_interface (key )
134+ # Pop the anchor string from ForceAnchor object
135+ force_anchor_str = force_anchor .pop () if force_anchor else None
121136 component_value = _component_func (
122137 anchor_ids = anchor_ids ,
123138 key = key ,
124139 anchor_icons = anchor_icons ,
125140 anchor_labels = anchor_labels ,
126- force_anchor = force_anchor ,
141+ force_anchor = force_anchor_str ,
127142 orientation = orientation ,
128143 override_styles = override_styles ,
129144 auto_update_anchor = auto_update_anchor ,
130145 disable_scroll = disable_scroll ,
131146 )
132- return component_value
147+ return component_value
0 commit comments