Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add marqueeOn prop to MarqueeController #2833

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion packages/ui/Marquee/MarqueeController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {forward} from '@enact/core/handle';
import hoc from '@enact/core/hoc';
import {Job} from '@enact/core/util';
import PropTypes from 'prop-types';
import React from 'react';

const STATE = {
Expand Down Expand Up @@ -46,6 +47,20 @@ const MarqueeController = hoc(defaultConfig, (config, Wrapped) => {
return class extends React.Component {
static displayName = 'MarqueeController'

static propTypes = /** @lends ui/Marquee.MarqueeController.prototype */ {
/**
* Determines what triggers the marquee to start its animation.
*
* This property is passed down to any chirdren Marquee components, and is used there,
* unless `marqueeOn` is specified for them as well.
*
* @type {('focus'|'hover'|'render')}
* @default 'focus'
* @public
*/
marqueeOn: PropTypes.oneOf(['focus', 'hover', 'render'])
}

constructor (props) {
super(props);

Expand All @@ -56,6 +71,7 @@ const MarqueeController = hoc(defaultConfig, (config, Wrapped) => {
complete: this.handleComplete,
enter: this.handleEnter,
leave: this.handleLeave,
marqueeOn: props.marqueeOn,
register: this.handleRegister,
start: this.handleStart,
unregister: this.handleUnregister
Expand Down Expand Up @@ -284,7 +300,7 @@ const MarqueeController = hoc(defaultConfig, (config, Wrapped) => {
}

render () {
let props = this.props;
let {...props} = this.props;

if (marqueeOnFocus) {
props = {
Expand All @@ -294,6 +310,8 @@ const MarqueeController = hoc(defaultConfig, (config, Wrapped) => {
};
}

delete props.marqueeOn;

return (
<MarqueeControllerContext.Provider value={this.childContext}>
<Wrapped {...props} />
Expand Down
24 changes: 14 additions & 10 deletions packages/ui/Marquee/MarqueeDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ const didPropChange = (propList, prev, next) => {
return hasPropsChanged.indexOf(true) !== -1;
};

const getMarqueeOn = function (props, context, marqueeOnDefault = 'focus') {
return (props.marqueeOn || (context && context.marqueeOn) || marqueeOnDefault);
};

/*
* There's only one timer shared for Marquee so we need to keep track of what we may be using it
* for. We may need to clean up certain things as we move among states.
Expand Down Expand Up @@ -304,7 +308,6 @@ const MarqueeDecorator = hoc(defaultConfig, (config, Wrapped) => {

static defaultProps = {
marqueeDelay: 1000,
marqueeOn: 'focus',
marqueeOnRenderDelay: 1000,
marqueeResetDelay: 1000,
marqueeSpacing: '50%',
Expand Down Expand Up @@ -338,7 +341,7 @@ const MarqueeDecorator = hoc(defaultConfig, (config, Wrapped) => {
}

this.validateTextDirection();
if (this.props.marqueeOn === 'render') {
if (getMarqueeOn(this.props, this.context) === 'render') {
this.startAnimation(this.props.marqueeOnRenderDelay);
}
on('keydown', this.handlePointerHide);
Expand All @@ -352,7 +355,7 @@ const MarqueeDecorator = hoc(defaultConfig, (config, Wrapped) => {
}

componentDidUpdate (prevProps) {
const {children, disabled, forceDirection, locale, marqueeOn, marqueeDisabled, marqueeSpacing, marqueeSpeed, rtl} = this.props;
const {children, disabled, forceDirection, locale, marqueeOn = getMarqueeOn(this.props, this.context), marqueeDisabled, marqueeSpacing, marqueeSpeed, rtl} = this.props;

let forceRestartMarquee = false;

Expand All @@ -371,7 +374,8 @@ const MarqueeDecorator = hoc(defaultConfig, (config, Wrapped) => {
this.invalidateMetrics();
this.cancelAnimation();
} else if (
prevProps.marqueeOn !== marqueeOn ||
// prevProps.marqueeOn !== marqueeOn ||
getMarqueeOn(prevProps, this.context) !== marqueeOn ||
prevProps.marqueeDisabled !== marqueeDisabled ||
prevProps.marqueeSpeed !== marqueeSpeed ||
prevProps.forceDirection !== forceDirection
Expand All @@ -383,7 +387,7 @@ const MarqueeDecorator = hoc(defaultConfig, (config, Wrapped) => {

this.validateTextDirection();
if (forceRestartMarquee || this.shouldStartMarquee()) {
this.tryStartingAnimation(this.props.marqueeOn === 'render' ? this.props.marqueeOnRenderDelay : this.props.marqueeDelay);
this.tryStartingAnimation(marqueeOn === 'render' ? this.props.marqueeOnRenderDelay : this.props.marqueeDelay);
}
}

Expand Down Expand Up @@ -462,7 +466,7 @@ const MarqueeDecorator = hoc(defaultConfig, (config, Wrapped) => {
* @returns {Boolean} - `true` if a possible marquee condition exists
*/
shouldStartMarquee () {
const {disabled, marqueeDisabled, marqueeOn} = this.props;
const {disabled, marqueeDisabled, marqueeOn = getMarqueeOn(this.props, this.context)} = this.props;
return !marqueeDisabled && (
marqueeOn === 'render' ||
!this.sync && (
Expand Down Expand Up @@ -769,15 +773,15 @@ const MarqueeDecorator = hoc(defaultConfig, (config, Wrapped) => {
forwardBlur(ev, this.props);
if (this.isFocused) {
this.isFocused = false;
if (!this.sync && !(this.isHovered && this.props.marqueeOn === 'hover')) {
if (!this.sync && !(this.isHovered && getMarqueeOn(this.props, this.context) === 'hover')) {
this.cancelAnimation();
}
}
}

handleEnter = (ev) => {
this.isHovered = true;
if (this.props.marqueeOn === 'hover') {
if (getMarqueeOn(this.props, this.context) === 'hover') {
if (this.sync) {
this.context.enter(this);
} else if (!this.state.animating) {
Expand All @@ -801,7 +805,7 @@ const MarqueeDecorator = hoc(defaultConfig, (config, Wrapped) => {

handleUnhover () {
this.isHovered = false;
if (this.props.marqueeOn === 'hover') {
if (getMarqueeOn(this.props, this.context) === 'hover') {
if (this.sync) {
this.context.leave(this);
} else {
Expand All @@ -826,7 +830,7 @@ const MarqueeDecorator = hoc(defaultConfig, (config, Wrapped) => {
alignment,
children,
disabled,
marqueeOn,
marqueeOn = getMarqueeOn(this.props, this.context),
marqueeSpeed,
...rest
} = this.props;
Expand Down