-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f2f3fc
commit c4c3dec
Showing
9 changed files
with
155 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import * as React from 'react'; | ||
|
||
export interface MountNodeProps { | ||
[key: string]: any; | ||
|
||
/** Additional classes. */ | ||
className?: string; | ||
} | ||
|
||
declare class MountNode extends React.Component<MountNodeProps, {}> { | ||
} | ||
|
||
export default MountNode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import PropTypes from 'prop-types' | ||
import React, {Component} from 'react' | ||
|
||
import {TYPES} from '../../lib/META' | ||
import handleChange from './lib/handleChange' | ||
import NodeRegistry from './lib/NodeRegistry' | ||
|
||
const nodeRegistry = new NodeRegistry() | ||
|
||
/** | ||
* Component. | ||
*/ | ||
export default class MountNode extends Component { | ||
static propTypes = { | ||
/** Primary content. */ | ||
className: PropTypes.string, | ||
|
||
node: PropTypes.node, | ||
} | ||
|
||
static _meta = { | ||
name: 'MountNode', | ||
type: TYPES.ADDON, | ||
} | ||
|
||
shouldComponentUpdate({className: next}) { | ||
const {className: current} = this.props | ||
|
||
return next !== current | ||
} | ||
|
||
componentWillMount() { | ||
const { node } = this.props | ||
|
||
nodeRegistry.add(node, this) | ||
nodeRegistry.emit(node, handleChange) | ||
} | ||
|
||
componentDidUpdate() { | ||
const { node } = this.props | ||
|
||
nodeRegistry.emit(node, handleChange) | ||
} | ||
|
||
componentWillUnmount() { | ||
const { node } = this.props | ||
|
||
nodeRegistry.del(node, this) | ||
nodeRegistry.emit(node, handleChange) | ||
} | ||
|
||
render() { | ||
return null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default, MountNodeProps } from './MountNode'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default from './MountNode' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
export default class NodeRegistry { | ||
constructor() { | ||
this.nodes = new Map() | ||
} | ||
|
||
add = (node, component) => { | ||
if(this.nodes.has(node)) { | ||
const set = this.nodes.get(node) | ||
|
||
set.add(component) | ||
return | ||
} | ||
|
||
const set = new Set() | ||
set.add(component) | ||
|
||
this.nodes.set(node, set) | ||
} | ||
|
||
del = (node, component) => { | ||
const set = this.nodes.get(node) | ||
|
||
if(set.size === 1) { | ||
this.nodes.delete(node) | ||
return | ||
} | ||
|
||
set.delete(component) | ||
} | ||
|
||
emit = (node, callback) => { | ||
callback(node, this.nodes.get(node)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import _ from 'lodash/fp' | ||
|
||
const computeClasses = (components) => components && _.flow( | ||
_.map('props.className'), | ||
_.filter(_.isString), | ||
_.map(_.split(/\s+/)), | ||
_.flatten, | ||
_.uniq, | ||
)([...components]) | ||
|
||
export default computeClasses |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import _ from 'lodash' | ||
import computeClasses from './computeClasses' | ||
|
||
let prevClasses = [] | ||
|
||
const handleChange = (node, components) => { | ||
const currentClasses = computeClasses(components) | ||
const forAdd = _.difference(currentClasses, prevClasses) | ||
const forRemoval = _.difference(prevClasses, currentClasses) | ||
|
||
_.forEach(forAdd, className => node.classList.add(className)) | ||
_.forEach(forRemoval, className => node.classList.remove(className)) | ||
|
||
prevClasses = currentClasses | ||
} | ||
|
||
export default handleChange |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters