bigskysoftware / _hyperscript

a small scripting language for the web
BSD 2-Clause "Simplified" License
3.12k stars 147 forks source link

PROPOSAL: Cookies #128

Closed benpate closed 1 year ago

benpate commented 3 years ago

Can we implement a way to add/remove cookies in hyperscript? This is similar to #127, but for stylesheets:

If we add expression cookie (similar to tag, input, or button from #121) that would identify a named cookie then we could do something like this:

set cookie "theme" to "dark" expiring NEVER

fetch /signin
set cookie "authorization" to it expiring 1 hour from now

remove cookie "theme"

Again, this is a hot take on giving hyperscript the ability to manage stylesheets dynamically, and I'm happy to keep looking for better options.

rtgoodwin commented 3 years ago

+1 on this. Would very much love to quickly drop in some lightweight preferences mgmt and it couldn't be easier to read than above ⬆️

1cg commented 3 years ago

I like it!

benpate commented 3 years ago

I looked at this a little bit tonight. It seems like the changes here should go into the runtime.resolveSymbol() and runtime.setSymbol() functions.

However, if we do that, it might be hard to include features like ... expiring tomorrow but perhaps that could be included/overridden in a configuration function named something like htmx.config.setCookie().

@1cg, do you agree? If so, I think I can make this update. Or, give me your guidance if you have other thoughts, and I'll happily adjust this plan.

1cg commented 3 years ago

I think this would be a new expression type, with the start cookie and then a custom syntax after that for declaring expiration, etc.

The expression itself would return the value of the cookie, and then you'd need to update set and put to handle setting values in the cookies (ideally by deferring to the cookie expression itself)

rtgoodwin commented 3 years ago

I'm rubbish at JS so I can't offer much here but excitement and thanks :) (and if you need some testing, I'm trying to launch a couple pages this weekend and could include a build; the basic feature is just remembering some filter preferences (probably an array or two) so if it failed gracefully it's no biggie.

benpate commented 3 years ago

@1cg -- I think I follow. I was heading down the road of making it a namespace. I'll aim at expressions and see where I get. Not totally sure I follow what the changes would be to the set statement, but let me knock it around for a bit first before I cry for help.

@rtgoodwin - cool, thanks. I'm not sure when I'll have something testable, but I'll reference this issue in the PR when I have something worth sharing. More eyes are always better!

medihack commented 2 years ago

Some cookie support in hyperscript would be great as it is one of the only reasons why I sometimes use Alpine.js + js-cookie instead of HTMX + hyperscript.

fatherofinvention commented 1 year ago

Hey @1cg do you think this is something that will eventually make it into Hyperscript?

1cg commented 1 year ago

how do people feel about this? image

fatherofinvention commented 1 year ago

@1cg I think that's perfect.