Skip to content

Commit

Permalink
Implemented marqueeOn prop support for MarqueeController
Browse files Browse the repository at this point in the history
Enact-DCO-1.0-Signed-off-by: Blake Stephens <[email protected]>
  • Loading branch information
Blake Stephens committed Feb 20, 2020
1 parent 1a0d28b commit 8fc2eaa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
5 changes: 4 additions & 1 deletion packages/ui/Marquee/MarqueeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,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 +285,7 @@ const MarqueeController = hoc(defaultConfig, (config, Wrapped) => {
}

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

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

delete props.marqueeOn;

return (
<MarqueeControllerContext.Provider value={this.childContext}>
<Wrapped {...props} />
Expand Down
31 changes: 19 additions & 12 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,15 +308,13 @@ const MarqueeDecorator = hoc(defaultConfig, (config, Wrapped) => {

static defaultProps = {
marqueeDelay: 1000,
marqueeOn: 'focus',
// marqueeOn: 'focus',
marqueeOnRenderDelay: 1000,
marqueeResetDelay: 1000,
marqueeSpacing: '50%',
marqueeSpeed: 60
}

static contextType = MarqueeControllerContext

constructor (props) {
super(props);
this.state = {
Expand All @@ -338,7 +340,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 +354,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 +373,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 +386,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 All @@ -402,6 +405,8 @@ const MarqueeDecorator = hoc(defaultConfig, (config, Wrapped) => {
off('keydown', this.handlePointerHide);
}

static contextType = MarqueeControllerContext

promoteJob = new Job(() => {
this.setState(state => state.promoted ? null : {promoted: true});
})
Expand Down Expand Up @@ -460,7 +465,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 @@ -745,15 +750,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 @@ -777,7 +782,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 @@ -802,11 +807,13 @@ const MarqueeDecorator = hoc(defaultConfig, (config, Wrapped) => {
alignment,
children,
disabled,
marqueeOn,
marqueeOn = getMarqueeOn(this.props, this.context),
marqueeSpeed,
...rest
} = this.props;

console.log('marqueeOn:', marqueeOn, this.context && this.context.marqueeOn, children);

const marqueeOnFocus = marqueeOn === 'focus';
const marqueeOnHover = marqueeOn === 'hover';
const marqueeOnRender = marqueeOn === 'render';
Expand Down

0 comments on commit 8fc2eaa

Please sign in to comment.