1

I have a composed page with multiple components. Like the picture, I want to show Component B inside Component A. By default, when clicking on A, hide B and show C in the middle. After clicking again, it toggles to show B and hide C.

When C has been showing, also change the A's border color.

enter image description here

import React from "react";
import styled, { css } from "styled-components";

import {
  ComponentA,
  ComponentB,
  ComponentC,
} from "~/components";

export const NewComponent: React.FC = ({
}) => {
  return (
    <>
      <ComponentA>
        <ComponentB>B</ComponentB>
        <ComponentC>C</ComponentC>
      </ComponentA>
    </>
  );
};

const ComponentA = styled()`
  ${({ theme }) => css`
    &:active {
      border-color: green;
      bordor: 1px solid;
    }
  `}
`;

I tried to use CSS to control the border color, it works with background-color but does not work for border-color. Also, I'm not sure if CSS can control the showing between Component B and component C.

3 Answers 3

1

To switch the components you need to use the useState hook with a boolean state. Then add an event onClick on the ComponentA and you can toggle state with the hook.

This way you can handle the border color switching. Create an attribute for ComponentA (you can chose name yourself ) you can pass values as a props, and using Ternary Operator toggle the color.

PS: If you don't use typescript, you can remove this line <{ color: "green" | "white" }> for ComponentA.

import { useState } from "react";
import styled from "styled-components";

export const ComponentA = styled.div<{ color: "green" | "white" }>`
  padding: 3rem 7rem;
  border: 2px solid ${(props) => props.color};
  border-radius: 0.5rem;
`;

const DefaultStyle = styled.div`
  max-width: 300px;
  padding: 3rem;
  border-radius: 0.5rem;
`;
export const ComponentB = styled(DefaultStyle)`
  background-color: magenta;
`;
export const ComponentC = styled(DefaultStyle)`
  background-color: orange;
`;

export default function App() {
  const [isActive, setIsActive] = useState(false);

  const toggle = () => setIsActive(!isActive);

  return (
    <div className="App">
      <ComponentA color={isActive ? "green" : "white"} onClick={toggle}>
        {isActive ? (
          <ComponentC>Component C</ComponentC>
        ) : (
          <ComponentB>Component B</ComponentB>
        )}
      </ComponentA>
    </div>
  );
}

Edit dazziling-code

Sign up to request clarification or add additional context in comments.

Comments

1
import {useState} from "react";

import {
  ComponentA,
  ComponentB,
  ComponentC,
} from "./components";

export const NewComponent: React.FC = ({
}) => {   

 const [isShowing, setIsShowing] = useState<boolean>(true);
            
              const toggle = () => {
                setIsShowing(current => !current);
              };
            
              return (
      <>
          <ComponentA>
                <div>
                  <button onClick={toggle}>Toggle</button>
                  {isShowing &&  <ComponentB>B</ComponentB>} // if true
                  {!isShowing && <ComponentC>C</ComponentC>} // if false
                </div>
     
          </ComponentA> </>
    
    )
}

1 Comment

Thank you. This way is awesome. I just need to move the button onclick event to ComponentA. For A's border color. Is there a good solution? Maybe CSS' active doesn't support border-color.
1
  1. bordor - you wrote it wrong. It's border
  2. When you set border: 1px solid; then border-color: initial

You can set CSS: border: 1px solid green;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.