The Challenge
here is the code for rainbow mode
.rainbow {
background: linear-gradient(in hsl longer hue 90deg, red 0 100%);
background-size: 200% auto;
background-clip: text;
animation: rainbow 10s linear infinite;
color: transparent;
}
the important line here is the background-clip: text what we do is draw the entire screen as a rainbow and then clip everything but the text.
Then we make the text transparent and all we are left with is the rainbow effect behind it. This however limits everything to being exclusively text
The UI
My first attempt to make buttons was just to use ascii boxes to draw around them which works but people expect that buttons change when hovered over and so it was confusing when they stayed the same.
The first thing I tried was changing which boxes I used (there are a plethora), but at the small scale it was hard to notice the difference, that's when I decided to have them change size as well. To do this I intentionally left a bunch of space around the default button which would be filled out by the full button.
const button = () => {
const buttonRows = [
//two rows here are nothing but space
` ${gap} `,
` ╭─${line}─╮ `,
` │ ${text} │ `,
` ╰─${line}─╯ `,
` ${gap} `,
//aditionally there is one space at the beginning and end of each string
];
return buttonRows.join("\n");
};
const hoverButton = () => {
const buttonRows = [
`╔──${line}──╗`,
`│ ${gap} │`,
`│ ${text} │`,
`│ ${gap} │`,
`╚──${line}──╝`,
];
return buttonRows.join("\n");
};
then it was as simple as hooking them up to the state and it works
Ideally they would work just via css but for now it's in JS. You have to pick your battles.
Changing buttons
Go ahead and press these buttons for a bit and see how they change text when you do
the problem I was facing was that as the label were changing the width of the buttons. I'm sure you're clever and realized that and are currently yelling at the screen saying just have a set width which I did do but that ended up making the buttons padded weirdly
After all that I arrived at this final iteration
the horrors
Now is time to show you the code that I think is very gross but is (to my understanding) the best solution here
<div style={{ position: "relative", left: "50%", top: "50%", width: "0", height: "0", display: "flex", justifyContent: "center", alignItems: "center", }} > <pre style={{ position: "relative", right: "50%", bottom: "50%", width: "fit-content", }} > {text} </pre> </div> {hover ? <pre>{hoverButton()}</pre> : <pre>{button()}</pre>}
This code will forever haunt my nightmares and I will do my best to explain why.
Unfortunately that's where I was stuck for the longest time
Other things I tried
position:absolute- the problem is that this breaks thebackground-clip:textof rainbow mode which is the whole reason we are in this mess in the first place- 'display:flex' - this would solve the problem except that once again either the button or the text have to be position absolute
Ultimately the conclusion I came to is that we need to use
position:relativeand the only real way to haveposition:relativenot cause weird spacing issues is by having it take up no space at all
Redemption
A week after I threw in the towel I discovered this site talking about how to center a div. At the time I was showing someone the depths of CSS but then I saw something but then I saw that you can actually stack elements into the same grid box and realized this was the nice clean solution I was looking for
<div style={{ display: "grid", placeContent: "center", placeItems: "center", }} > <div style={{ gridRow: "1", gridColumn: "1", }} > <pre>{text}</pre> </div> {hover ? ( <pre style={{ gridRow: "1", gridColumn: "1", }} > {hoverButton()} </pre> ) : ( <pre style={{ gridRow: "1", gridColumn: "1", }} > {button()} </pre> </div>
and while yes it is certainly more code than before it is more accurate to what I want which is 'make one box and shove everything into it' I think that's a lesson I keep learning, that if you have to wrestle the CSS too much it is likely that taking a more declarative approach will pay off in the end