React Buttons not working, only using fallback styles - css

I'm new to React, but following multiple guides I have an issue with buttons not selecting the correct style based on "checkButtonStyle", only rendering with the fallback options.
My code is:
(Button.jsx)
import React from 'react';
import './Button.css';
const STYLES = ['btn--primary', 'btn--light', 'btn--dark', 'btn--outline', 'btn--outline--light', 'btn--outline--dark'];
const SIZES = ['btn--medium', 'btn--large'];
export const Button = ({children, type, onClick, buttonStyle, buttonSize}) => {
const checkButtonStyle = STYLES.includes(buttonStyle) ? buttonStyle : STYLES[0];
const checkButtonSize = SIZES.includes(buttonSize) ? buttonSize : SIZES[0];
return (
<button className={'btn ${checkButtonStyle} ${checkButtonSize}'} onClick={onClick} type={type}>
{children}
</button>
)
};
(Button.css)
:root {
--primary: #EF1B71;
--light: #FFFFFF;
}
.btn {
font-family: 'Lato', sans-serif;
font-weight: 400;
border-radius: 4px;
cursor: pointer;
transition: 500ms ease;
}
.btn--primary {
background-color: var(--primary);
color: var(--light);
border: 1px solid var(--primary);
}
.btn--medium {
padding: 8px 20px;
}
(HeroSection)
import React from 'react';
import '../App.css';
import { Button } from './Button';
import './HeroSection.css';
function HeroSection() {
return (
<div className='hero-container'>
<video src='/videos/home-hero-video-2.mp4' autoPlay loop muted />
<div className="hero-btns">
<Button type='button' buttonStyle='btn--primary' buttonSize="btn--medium">Enquire</Button>
</div>
</div>
)
}
I have tried everything I can think of, with no errors showing in console for the button, however the only result I am getting is the .btn style within Button.css and not the btn--primary or btn--medium styles.
Thank you

className={
checkButtonSizes + " " + checkButtonStyle + " btn"}
You have to pass the variables in the format mentioned above to make it work. It works for me. Please try and let me know.

In JavaScript, it's possible to use variables in strings with a JavaScript template literal.
However, this requires the use of backticks (`) rather than single (') or double (") quotes.
This line uses single quotes rather than backticks. Thus, ${checkButtonStyle} and ${checkButtonSize} will be rendered as strings and not their assigned values.
<button className={'btn ${checkButtonStyle} ${checkButtonSize}'} onClick={onClick} type={type}>

Related

Styled component: Nested rules in different files does not work

I've defined a file with a styled textarea but I want to use a different piece of style for a React component, at the end I am not able to change the style for the particular tag.
Here below the textarea file
const Container = styled.div`
margin-bottom: 2rem;
`;
const Label = styled(B1)`
margin-bottom: 0.5rem;
`;
const StyledTextArea = styled.textarea`
width: fill-available;
height: 2rem;
padding: 0.25rem;
background-color: black;
font-size: 1rem;
font-weight: 400;
border: none;
border-radius: 0.25rem;
::placeholder {
color: red;
}
`;
const TextArea = ({ label, placeholder, input, rows }) => (
<Container>
{label && <Label>{label}</Label>}
<StyledTextArea rows={rows} {...input} placeholder={placeholder} />
</Container>
);
TextArea.propTypes = {
label: PropTypes.string,
placeholder: PropTypes.string,
input: PropTypes.object.isRequired
};
export default TextArea;
Here below there is my component in which I'd like to change the font-size component but it does not work
const TextBox = styled(TextArea)`
> StyledTextArea {
font-size: 3rem !important;
}
`
class SingleGuestForm extends Component {
<Field
name="Nested Rules"
label="Test with nested rules"
rows="3"
component={TextBox}
/>
}
It seems that the rule defined in the TextBox is ignored by the style engine. Do you know why it happens? I tried different solution (surrounding StyledTextArea with curly braces, etc) but nothing happen even if in the official doc the solution I've implemented should work (here). Thank you so much for any help

input[type="search"]::-webkit-search-decoration:hover {cursor: pointer;} in styled-components

like the title says, how do I use input[type="search"]::-webkit-search-decoration:hover {cursor: pointer;} in a styled component?...
the code below isn't working or being recognized.
const SomeInput = styled.input`
border-radius: 8px;
input[type="search"]::-webkit-search-decoration:hover,
input[type="search"]::-webkit-search-cancel-button:hover {
cursor:pointer;
}
`;
Trying to implement from here as a styled component: display pointer on hover close icon in search textbox
I got this working by using the ampersand character inside the styled component and added type="search" in the JSX.
import React from "react";
import "./styles.css";
import styled from "styled-components";
export default function App() {
const SomeInput = styled.input`
border-radius: 8px;
&::-webkit-search-decoration:hover,
&::-webkit-search-cancel-button:hover {
cursor: pointer;
}
`;
return (
<div className="App">
<SomeInput type="search" />
</div>
);
}
CodeSandbox

react how to style : styled-dropdown-component

I am trying to change the color and the size of the DropdownMenu using the styled-components like the code below:
const DropdownCustom = styled.DropdownMenu`
font-family: sans-serif;
font-size: 1.3rem;
border: none;
border-radius: 5px;
background-color: red;
`;
Then I try to use it like this:
<Dropdown>
<button onClick={() => setState(!state)}>Create</button>
<DropdownCustom hidden={!state}>
<DropdownItem>Action</DropdownItem>
<DropdownItem>Another action</DropdownItem>
<DropdownItem>Something else here</DropdownItem>
</DropdownCustom>
</Dropdown>
But it gives me an error saying that _styledComponents.default.DropdownMenu is not a function.
I am very new to styling with css and it is very confusing, so any advice or guide would be really appreciated! :)
Edited
import {
Dropdown,
DropdownItem,
DropdownMenu
} from "styled-dropdown-component";
If you trying to style a custom component you need to use styled as a function:
const DropdownCustom = styled(DropdownMenu)`
font-family: ...
`;
It will work only if the custom component uses the className props.
Therefore, you sometimes want to style custom component's parent, and target the styling with selectors - as it's just a CSS:
const Wrapper = styled.div`
font-family: ...;
.dropDown {
....;
}
`;
<Wrapper>
<Dropdown />
</Wrapper>

Share the same styles between two types of component with React Styled Components

I would like to apply exactly the same styles to a styled input element and a styled select element.
Currently, I'm using string interpolation to do this:
const styles = `
background-color: white;
width: 100%;
border: 0 solid transparent;
border-radius: 10px;
margin-bottom: 20px;
padding-top: 5px;
padding-bottom: 5px;
font-size: 1.2rem;
`
const Select = styled.select`${styles}`
const Input = styled.input`${styles}`
Is there a better way of doing this which doesn't involve using a 'raw' string? The disadvantage of using the raw styles string is that Visual Studio Code doesn't syntax-highlight it:
You have few options here:
css helper function:
const styles = css`
background-color: white;
// ...
`;
const Select = styled.select`${styles}`;
const Input = styled.input`${styles}`;
"as" polymorphic prop (added in v4):
<Select as="input">
...
</Select>
withComponent method (candidate for deprecation):
const Select = styled.select`
background-color: white;
// ...
`;
const Input = Select.withComponent('input');
You can use the css tagged template literal:
import styled, { css } from "styled-components";
const styles = css`
background-color: white;
width: 100%;
`;
const Select = styled.select`${styles}`;
const Input = styled.input`${styles}`;
That should get properly syntax highlighted (haven't tested).

Using styled-components with gatsbyJS

i am using gatsby-plugin-styled-components to style the elements below.
import React from 'react';
import styled from 'styled-components';
const secondChar = styled.span`
font-size: 3em;
color: azure;
`
const TitleWrapper = styled.div`
font-size: 3.5em;
font-weight: 100;
letter-spacing: 0.01em;
font-family: 'Covered By Your Grace', cursive;
`
const Title = () => (
<TitleWrapper>
<span>a</span>
<secondChar>b</secondChar>
<span>c</span>
<span>e</span>
<span>f</span>
<span>g</span>
<span>h</span>
</TitleWrapper>
)
export default Title;
For some reason i simply cant figure out myself, i am unable to style the secondChar component. Color and font size doesn't change at all.
However, i am able to style the secondChar via Chrome Dev Tool.
Anyone can advise what is going on?
Thank you.
updates:
Solved the first issue above. Forgot to use camelcase for components.
now i am trying to implement the following
Styled-components: refering to other components
const SecondChar = styled.span`
display: inline-block;
transform: translateX(20px);
transition: transform 500ms ease-in-out;
${TitleWrapper}:hover & {
color: azure;
}
`
const TitleWrapper = styled.div`
font-size: 3.5em;
font-weight: 100;
letter-spacing: 0.01em;
font-family: 'Covered By Your Grace', cursive;
`
const Title = () => (
<TitleWrapper>
<span>a</span>
<SecondChar>b</SecondChar>
<span>c</span>
<span>d</span>
<span>e</span>
<span>f</span>
<span>g</span>
</TitleWrapper>
)
however hovering over TitleWrapper doesn't have any response on the SecondChar component. am i doing something wrong again?
All component naming should start by capital letter, including styled components, so the secondChar should be SecondChar

Resources