Toggle Switch Demo (Custom HTML)

Version History :
v0.2 - fixed CSS so non-toggleswitch checkboxes were not affected by the pseudo-classes.
v0.5 - now has square and rounded switches.
v0.6 - now a custom component!


Here’s a demo of an animated toggle switch, made using CSS & custom HTML forms and wrapped around with a demo project.

image

image

To use it you can drag the “Toggleswitch” form onto your project form. Hopefully the demo project will show you most of the functionality.

Try it here :
https://E7OU2TUOU5J32Q2Q.anvilapp.net/NXQ5JR67IJYCCLI3LNW7JEPO

Clone it from here :
https://anvil.works/ide#clone:E7OU2TUOU5J32Q2Q=YYAKUWIWKBUCX3QYZBN6N3A4

3 Likes

Open to improvements … :slight_smile:

Keep an eye on the version numbers in the revision history in the original post.
If your cloned demo project form disagrees with it then please re-clone.

Thanks bud! This is sweet.

Please re-clone : now at v0.6 (custom component).

1 Like

Hmm, I’m having trouble getting this to work. I have it as a dependency and the custom component shows up in the design IDE. But when I add one to the form and run the app, it just displays as a normal checkbox. Any idea what I could be doing wrong?

Hi,
sorry I’m away on holiday until 3rd September.

If no one else can help you by then I’ll jump on it on my return.

1 Like

Sharing the app (if possible) may help others to assist more easily.

Right, thanks. Here’s a simple app that reproduces the problem: https://anvil.works/build#clone:KMAFRYEJBH4FW63S=MSV7WCZZ3PJVESRWO3RXM6W4

All I did was add an unchanged clone of the Toggle Switch Demo v0.6 above as a dependency, and then drag a Toggleswitch custom component onto the form.

1 Like

Okay, I think you just needed to add David’s CSS and HTML assets.

I added them and I get the nicely styled toggle switch and output that likely means it is working.

Clone to demonstrate (note that you may have to adjust your dependency name to match):
https://anvil.works/build#clone:ASLFGNAVOW4UXHY3=D6EC73PSZZQSNKGEPBWFS3XF

I hope this helps.

1 Like

Ok, I see. Thanks so much! That fixes it for me.

But doesn’t it seem like custom components shouldn’t require this manual copying of assets? The custom css I can see, I guess, but the custom component itself is a custom html form, so it seems odd that I would need to manually copy that html file to use it, no? Would this make sense as a feature request?

@shaun may be able to answer that and the related questions.

1 Like

In an unrelated issue, I was surprised that setting the “checked” property programmatically would trigger the “x_change” event the same way that clicking on the toggle does. That is not how plain checkboxes behave. So I changed that to the behavior I expected by editing the following method of the Toggleswitch class, commenting out the line that triggers the event when “checked” is set programmatically:

  @checked.setter
  def checked(self,value):
    if type(value) is bool:
      self._checked=value
      self.update()
      #self.check_box_1_change()