简体   繁体   English

如何使用带有CSS-in-JS的React重写此按钮及其悬停效果

[英]How can I rewrite this button with its hover effect using React with CSS-in-JS

I wanted to achieve a button with effect like this https://codepen.io/electerious/pen/rroqdL and wanted it to make it a React component. 我想要实现一个具有https://codepen.io/electerious/pen/rroqdL这样的效果的按钮,并希望使其成为React组件。

The original example is using css variable so I thought it would be easier to implement it using CSS-in-JS, in my case I chose to use Emotion( https://emotion.sh ). 原始示例使用css变量,因此我认为使用CSS-in-JS来实现它会更容易,在我的情况下,我选择使用Emotion( https://emotion.sh )。

First, this is my current code 首先,这是我当前的代码

import React from "react";
import { css } from "@emotion/core";

const style = css`
  a {
    position: relative;
    display: inline-block;
    padding: 1.2em 2em;
    text-decoration: none;
    text-align: center;
    cursor: pointer;
    user-select: none;
    color: white;

    &::before {
      content: "";
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      background: linear-gradient(135deg, #6e8efb, #a777e3);
      border-radius: 4px;
      transition: box-shadow 0.5s ease, transform 0.2s ease;
      will-change: transform;
      box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
      transform: translateY(var(--ty, 0)) rotateX(var(--rx, 0))
        rotateY(var(--ry, 0)) translateZ(var(--tz, -12px));
    }

    &:hover::before {
      box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
    }

    &::after {
      position: relative;
      display: inline-block;
      content: attr(data-title);
      transition: transform 0.2s ease;
      font-weight: bold;
      letter-spacing: 0.01em;
      will-change: transform;
      transform: translateY(var(--ty, 0)) rotateX(var(--rx, 0))
        rotateY(var(--ry, 0));
    }
  }
`;

const Button = () => {
  return (
    <div css={style}>
      <a href="#" dataTitle="Awesome Button">
        Button
      </a>
    </div>
  );
};

export default Button; 导出默认按钮;

However I have encountered two difficulties. 但是我遇到了两个困难。

  1. according the original example, it manipulates the <a/> tag directly in the js file using const aElem = document.querySelector('a') const boundingClientRect = aElem.getBoundingClientRect() . 根据原始示例,它使用const aElem = document.querySelector('a') const boundingClientRect = aElem.getBoundingClientRect()直接在js文件中操作<a/>标记。 I am not sure how I can achieve this using React because it seems like I don't know how to directly access the DOM element I am about to render in React. 我不确定如何使用React实现此目的,因为似乎我不知道如何直接访问要在React中渲染的DOM元素。

  2. Again, the original example uses const docStyle = document.documentElement.style . 同样,原始示例使用const docStyle = document.documentElement.style I don't know the equivalent in React. 我不知道React中的等效项。

I am new to React and even CSS-In-JS in general. 我是React甚至CSS-In-JS的新手。 I would greatly appreciate it if someone can code out this example using React. 如果有人可以使用React编写此示例,我将不胜感激。

You can actually do this using the useRef (see the documentation for hooks and more specifically the useRef hook here - https://reactjs.org/docs/hooks-reference.html#useref ) hook without changing much of the code, not sure if you should do it this way but it works? 实际上,你可以做到这一点使用的useRef (见钩这里的文档,更具体的useRef钩- https://reactjs.org/docs/hooks-reference.html#useref )钩在不改变代码的多,不知道如果您应该以这种方式这样做,但它可以工作吗?

I have used an external SCSS file rather than emotion but you should be able to replicate pretty easily. 我使用的是外部SCSS文件而不是情感文件,但您应该能够轻松复制。

Here is the working example: https://codesandbox.io/s/cocky-neumann-kik51?fontsize=14 这是工作示例: https : //codesandbox.io/s/cocky-neumann-kik51?fontsize=14

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM