Best JavaScript code snippet using storybook-root
calc.js
Source: calc.js
1const html = document.documentElement;23if (localStorage.fontSize) {4 html.style.fontSize = localStorage.fontSize;5}67const multiplyButton = document.querySelector("#multiply"),8 divideButton = document.querySelector("#divide"),9 incrementButton = document.querySelector("#increment"),10 decrementButton = document.querySelector("#decrement"),11 equalsButton = document.querySelector("#equals"),12 cropButton = document.querySelector("#slice"),13 input = document.querySelector("input"),14 settings = document.querySelector(".calc__controls"),15 buttonOpenSettings = document.querySelector("#open-settings"),16 buttonCloseSettings = document.querySelector(".calc__controls-close"),17 selectForTheme = document.querySelector("#theme"),18 scaleWrapper = document.querySelector(".scale-wrapper"),19 defaultSettingsButton = document.querySelector("#default-settings"),20 gradientButton = document.querySelector("#with-gradient");2122multiplyButton.addEventListener("click", () => {23 input.value += " * ";24});2526divideButton.addEventListener("click", () => {27 input.value += " / ";28});2930incrementButton.addEventListener("click", () => {31 input.value += " + ";32});3334decrementButton.addEventListener("click", () => {35 input.value += " - ";36});3738cropButton.addEventListener("click", () => {39 input.value = input.value.slice(0, -1);40});4142equalsButton.addEventListener("click", () => {43 let script = document.createElement("script");44 script.text = `45 try {46 input.value = ${input.value};47 } catch (error) { }48 `;4950 document.head.append(script);51 script.text = "";52 script.remove();53});5455document.querySelectorAll(".calc button").forEach(button => {56 button.addEventListener("click", (event) => {57 if (!isNaN(event.target.innerHTML)) {58 input.value += event.target.innerHTML.trim();59 }60 });61});6263input.addEventListener("keydown", (event) => {64 if (event.key === "Enter") {65 let selection = input.selectionStart;66 equalsButton.click();67 input.selectionStart = input.selectionEnd = selection;68 translateTrueAndFalse(event.target);69 }70});7172input.addEventListener("input", (event) => {73 translateTrueAndFalse(event.target);74});7576function translateTrueAndFalse(element) {77}7879// Dark / white theme (or with gradient)80selectForTheme.addEventListener("input", function() {81 html.dataset.theme = (selectForTheme.value);82 localStorage.theme = selectForTheme.value;83});8485if (localStorage.theme) {86 html.dataset.theme = localStorage.theme;87 selectForTheme.value = localStorage.theme;88}8990gradientButton.addEventListener("input", () => {91 if (gradientButton.querySelector("input").checked) {92 html.setAttribute("with-gradient", "");93 localStorage.setItem("withGradient", true);94 } else if (!gradientButton.querySelector("input").checked) {95 html.removeAttribute("with-gradient");96 localStorage.setItem("withGradient", false);97 }98});99100if (localStorage.withGradient === "true") {101 html.setAttribute("with-gradient", "");102 gradientButton.querySelector("input").checked = "checked";103}104105if (localStorage.withGradient === "false") {106 gradientButton.querySelector("input").removeAttribute("checked");107}108109// General settings110buttonOpenSettings.addEventListener("click", () => {111 settings.classList.remove("hide");112 settings.classList.add("show");113 setTimeout(() => {114 settings.style.display = "block";115 buttonOpenSettings.style.display = "none";116 });117});118119buttonCloseSettings.addEventListener("click", () => {120 settings.classList.add("hide");121 settings.classList.remove("show");122 setTimeout(() => {123 settings.style.display = "";124 buttonOpenSettings.style.display = "";125 }, 100);126});127128defaultSettingsButton.addEventListener("click", () => {129 localStorage.fontSize = "";130 localStorage.theme = "white";131 localStorage.withGradient = false;132 document.location.reload();133});134135// Scale UI136let rem = +window.getComputedStyle(html).fontSize.replace("px", "");137scaleWrapper.addEventListener("click", (event) => {138 if (event.target === scaleWrapper.querySelector("#decrement") ||139 event.target === scaleWrapper.querySelector("#increment")) {140 // event.target.id === "decrement" || "increment"141 if (event.target.id === "decrement") {142 rem++;143 html.style.fontSize = rem + "px";144 }145146 if (event.target.id === "increment") {147 rem--;148 html.style.fontSize = rem + "px";149 }150151 if (event.target.id === "decrement" || "increment") {152 localStorage.setItem("fontSize", `${rem}px`);153 }154 }
...
Sidebar.js
Source: Sidebar.js
1import React, { useRef } from 'react'2import PropTypes from 'prop-types'3import SimpleBar from 'simplebar-react'4import 'simplebar/dist/simplebar.min.css'5import { makeStyles, Drawer, Hidden } from '@material-ui/core'6import { Typography } from '@bit/totalsoft_oss.react-mui.kit.core'7import { useTranslation } from 'react-i18next'8import { env } from 'utils/env'9import Menu from 'components/menu/Menu'10import UserMenu from '../menu/UserMenu'11import sidebarStyle from 'assets/jss/components/sidebarStyle'12import cx from 'classnames'13import { withRouter } from 'react-router-dom'14import { sidebarWrapperHeight } from 'utils/constants'15const useStyles = makeStyles(sidebarStyle)16// We've created this component so we can have a ref to the wrapper of the links that appears in our sidebar.17// This was necessary so that we could initialize PerfectScrollbar on the links.18// There might be something with the Hidden component from material-ui, and we didn't have access to19// the links, and couldn't initialize the plugin.20function SidebarWrapper({ className, children }) {21 const sidebarWrapperRef = useRef()22 return (23 <div className={className} ref={sidebarWrapperRef}>24 <SimpleBar style={{ height: sidebarWrapperHeight, overflowX: 'hidden' }}>{children}</SimpleBar>25 </div>26 )27}28SidebarWrapper.propTypes = {29 className: PropTypes.string.isRequired,30 children: PropTypes.array.isRequired31}32function Sidebar({ logo, logoText, drawerOpen, changeLanguage, closeDrawer, withGradient }) {33 const classes = useStyles()34 const { i18n, t } = useTranslation()35 const logoNormal =36 classes.logoNormal +37 ' ' +38 cx({39 [classes.logoNormalSidebarMini]: !drawerOpen40 })41 var brand = (42 <div className={classes.logo}>43 <a href='/' className={classes.logoMini}>44 <img src={logo} alt='logo' className={classes.img} />45 </a>46 {logoText && (47 <a href='/' className={logoNormal}>48 {logoText}49 </a>50 )}51 </div>52 )53 var appVersion = (54 <Typography className={classes.appVersion} variant={'caption'}>55 {`${t('BuildVersion')} ${env.REACT_APP_VERSION}`}56 </Typography>57 )58 const drawerPaper =59 classes.drawerPaper +60 ' ' +61 cx({62 [classes.drawerPaperMini]: !drawerOpen63 })64 const sidebarWrapper =65 classes.sidebarWrapper +66 ' ' +67 cx({68 [classes.drawerPaperMini]: !drawerOpen69 })70 return (71 <div>72 <Hidden mdUp>73 <Drawer74 variant='temporary'75 anchor='right'76 open={drawerOpen}77 classes={{78 paper: drawerPaper + ' ' + classes.themeBackground79 }}80 onClose={closeDrawer}81 ModalProps={{82 keepMounted: true // Better open performance on mobile.83 }}84 >85 {brand}86 <SidebarWrapper className={sidebarWrapper}>87 <UserMenu88 drawerOpen={drawerOpen}89 changeLanguage={changeLanguage}90 language={i18n.language}91 withGradient={withGradient}92 />93 <Menu drawerOpen={drawerOpen} withGradient={withGradient} />94 </SidebarWrapper>95 {appVersion}96 </Drawer>97 </Hidden>98 <Hidden smDown>99 <Drawer100 anchor='left'101 variant='permanent'102 open={drawerOpen}103 classes={{104 paper: drawerPaper + ' ' + classes.themeBackground105 }}106 >107 {brand}108 <SidebarWrapper className={sidebarWrapper}>109 <UserMenu110 drawerOpen={drawerOpen}111 changeLanguage={changeLanguage}112 language={i18n.language}113 withGradient={withGradient}114 />115 <Menu drawerOpen={drawerOpen} withGradient={withGradient} />116 </SidebarWrapper>117 {appVersion}118 </Drawer>119 </Hidden>120 </div>121 )122}123Sidebar.propTypes = {124 drawerOpen: PropTypes.bool.isRequired,125 closeDrawer: PropTypes.func.isRequired,126 changeLanguage: PropTypes.func.isRequired,127 logo: PropTypes.string,128 logoText: PropTypes.string,129 withGradient: PropTypes.bool.isRequired130}131//router is needed for re-rendering...
SubscribeForm.js
Source: SubscribeForm.js
1import React, { useState } from "react"2import addToMailchimp from "gatsby-plugin-mailchimp"3// import { GradientBorderPurple } from "../ui-components"4export const SubscribeForm = ({ modal, ...props }) => {5 const [firstName, setFirstName] = useState("")6 const [email, setEmail] = useState("")7 const [msg, setMsg] = useState()8 const handleSubmit = (e) => {9 e.preventDefault()10 addToMailchimp(email, { FNAME: firstName }).then((data) => {11 return data.result === "success"12 ? setMsg(13 "Thank you for subscribing! we have sent you a confirmation email with your free theme."14 )15 : setMsg("This email has already subscribed, try with another one")16 })17 }18 // const WithGradient = ({ children }) => {19 // return modal ? (20 // <GradientBorderPurple className="p-[3px] mb-5 rounded-md">21 // {children}22 // </GradientBorderPurple>23 // ) : (24 // children25 // )26 // }27 const styledInput = `input-focus bg-orange-50 shadow-sm rounded-md text-text mb-5 ${28 modal ? "border-2 border-gold" : "border-0"29 }`30 return (31 <>32 {msg ? (33 <div34 className={`text-lg ${!modal ? "text-purple-200 my-5" : "text-text"}`}35 >36 {msg}37 </div>38 ) : (39 <form onSubmit={handleSubmit} {...props}>40 {/* <WithGradient> */}41 <input42 type="text"43 aria-label="first name"44 placeholder="First Name"45 value={firstName}46 required47 onChange={(e) => setFirstName(e.target.value)}48 className={styledInput}49 />50 {/* </WithGradient> */}51 {/* <WithGradient> */}52 <input53 type="email"54 aria-label="email"55 placeholder="Email address"56 onChange={(e) => setEmail(e.target.value)}57 value={email}58 className={styledInput}59 required60 />61 {/* </WithGradient> */}62 <div className="flex justify-end">63 <div>64 <button className="h-12 btn btn-primary btn-large" type="submit">65 Subscribe66 </button>67 </div>68 </div>69 </form>70 )}71 </>72 )...
Using AI Code Generation
1import React from 'react';2import { storiesOf } from '@storybook/react';3import { action } from '@storybook/addon-actions';4import { linkTo } from '@storybook/addon-links';5import { withInfo } from '@storybook/addon-info';6import { WithGradient } from 'storybook-root';7storiesOf('WithGradient', module)8 .add('Default', () => (9 ));10@import "~storybook-root/src/WithGradient.css";11import React from 'react';12import { storiesOf } from '@storybook/react';13import { action } from '@storybook/addon-actions';14import { linkTo } from '@storybook/addon-links';15import { withInfo } from '@storybook/addon-info';16import { WithGradient } from 'storybook-root';17storiesOf('WithGradient', module)18 .add('Default', () => (19 ));20@import "~storybook-root/src/WithGradient.scss";21import React from 'react';22import { storiesOf } from '@storybook/react';23import { action } from '@storybook/addon-actions';24import { linkTo } from '@storybook/addon-links';25import { withInfo } from '@storybook/addon-info';26import { WithGradient } from 'storybook-root';27storiesOf('WithGradient', module)28 .add('Default', () => (29 ));30import React from 'react';31import { storiesOf } from '@storybook/react';32import { action } from '@storybook/addon-actions';33import { linkTo } from '@storybook/addon-links';34import { withInfo } from '@storybook/addon-info';35import { WithGradient } from 'storybook-root';36storiesOf('WithGradient', module)37 .add('Default', () => (38 ));39import React from 'react';40import { storiesOf } from '@storybook/react';41import { action } from '@storybook/add
Using AI Code Generation
1import React from 'react';2import { storiesOf } from '@storybook/react';3import { WithGradient } from '../src';4storiesOf('WithGradient', module).add('default', () => (5));6import React from 'react';7import { Gradient } from './Gradient';8export const WithGradient = ({ children }) => (9 {children}10);11import React from 'react';12export const Gradient = ({ children }) => (13 {children}14);
Check out the latest blogs from LambdaTest on this topic:
Hey everyone! We hope you had a great Hacktober. At LambdaTest, we thrive to bring you the best with each update. Our engineering and tech teams work at lightning speed to deliver you a seamless testing experience.
In today’s world, an organization’s most valuable resource is its customers. However, acquiring new customers in an increasingly competitive marketplace can be challenging while maintaining a strong bond with existing clients. Implementing a customer relationship management (CRM) system will allow your organization to keep track of important customer information. This will enable you to market your services and products to these customers better.
When software developers took years to create and introduce new products to the market is long gone. Users (or consumers) today are more eager to use their favorite applications with the latest bells and whistles. However, users today don’t have the patience to work around bugs, errors, and design flaws. People have less self-control, and if your product or application doesn’t make life easier for users, they’ll leave for a better solution.
Estimates are critical if you want to be successful with projects. If you begin with a bad estimating approach, the project will almost certainly fail. To produce a much more promising estimate, direct each estimation-process issue toward a repeatable standard process. A smart approach reduces the degree of uncertainty. When dealing with presales phases, having the most precise estimation findings can assist you to deal with the project plan. This also helps the process to function more successfully, especially when faced with tight schedules and the danger of deviation.
When I started writing tests with Cypress, I was always going to use the user interface to interact and change the application’s state when running tests.
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!