MUI - 如何在悬停时为卡片深度设置动画?

MUI - How to animate Card depth on hover?

我想在鼠标悬停时对整个 Card 的深度进行动画处理。 我尝试了这个(所以我是 React 的新手)但我不知道该怎么做:

<Card 
  linkButton={true}
  href="/servicios/"
  onClick={Link.handleClick} zDepth={3}
  onMouseEnter={this.setState({zDepth={1}})}>
</Card>

提前致谢。

constructor(props) {
  super(props);
  this.state = { shadow: 1 }
}

onMouseOver = () => this.setState({ shadow: 3 });
onMouseOut = () => this.setState({ shadow: 1 });

<Card 
   onMouseOver={this.onMouseOver}
   onMouseOut={this.onMouseOut}
   zDepth={this.state.shadow}
>

更新#1

完整示例

// StyledCard.js

import React, { Component } from 'react';
import { Card } from 'material-ui/Card';

class StyledCard extends Component {
  state: {
    shadow: 1
  }

  onMouseOver = () => this.setState({ shadow: 3 });

  onMouseOut = () => this.setState({ shadow: 1 });

  render() {
    return (
      <Card 
        onMouseOver={this.onMouseOver}
        onMouseOut={this.onMouseOut}
        zDepth={this.state.shadow}
      >
        {this.props.children}
      </Card>
    );
  }

export default StyledCard;

.

// Container.js

import React from 'react';
import StyledCard from './StyledCard';

const Container = () => [
  <StyledCard>Card 1</StyledCard>,
  <StyledCard>Card 2</StyledCard>,
  <StyledCard>Card 3</StyledCard>,
];

export default Container;

更新#2

有 HOC

// withShadow.js

import React from 'react';

const withShadow = (Component, { init = 1, hovered = 3 }) => {
  return class extends React.Component {
    state: {
      shadow: init
    };

    onMouseOver = () => this.setState({ shadow: hovered });

    onMouseOut = () => this.setState({ shadow: init });

    render() {
      return (
        <Component
          onMouseOver={this.onMouseOver}
          onMouseOut={this.onMouseOut}
          zDepth={this.state.shadow}
          {...this.props}
        />
      );
    }
  };
};

export default withShadow;

.

// Container.js

import React from 'react';
import { Card } from 'material-ui/Card';
import withShadow from './withShadow';

const CardWithShadow = withShadow(Card, { init: 2, hovered: 4 });

const Container = () => [
  <CardWithShadow>Card 1</CardWithShadow>,
  <CardWithShadow>Card 2</CardWithShadow>,
  <CardWithShadow>Card 3</CardWithShadow>,
];

export default Container;

@Alex Sandiiarov 的回答对我不起作用。文档显示使用 raised 属性。 https://material-ui.com/api/card/

class Component extends React.Component{

  state = {
    raised:false
  }

  toggleRaised = () => this.setState({raised:!this.state.raised});

  render(){
    return <Card onMouseOver={this.toggleRaised} 
      onMouseOut={this.toggleRaised} 
      raised={this.state.raised}>

      ...

      </Card>

  }
}

5年过去了,还是没有正确答案,悬停的时候不用设置组件状态,直接用伪class :hover:

<Card
  sx={{
    ':hover': {
      boxShadow: 20, // theme.shadows[20]
    },
  }}
>

如果你想使用styled():

const options = {
  shouldForwardProp: (prop) => prop !== 'hoverShadow',
};
const StyledCard = styled(
  Card,
  options,
)(({ theme, hoverShadow = 1 }) => ({
  ':hover': {
    boxShadow: theme.shadows[hoverShadow],
  },
}));
<StyledCard hoverShadow={10}>
  <Content />
</StyledCard>

现场演示