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

Adding support for server-side rendering #15

Merged
merged 4 commits into from
Jun 17, 2015
Merged
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
106 changes: 60 additions & 46 deletions dist/react-tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,52 @@ var ReactTooltip = _react2['default'].createClass({
place: _react2['default'].PropTypes.string,
type: _react2['default'].PropTypes.string,
effect: _react2['default'].PropTypes.string,
positon: _react2['default'].PropTypes.object },
positon: _react2['default'].PropTypes.object
},

_updatePosition: function _updatePosition() {
var tipWidth = document.querySelector('[data-id=\'tooltip\']') ? document.querySelector('[data-id=\'tooltip\']').clientWidth : 0;
var tipHeight = document.querySelector('[data-id=\'tooltip\']') ? document.querySelector('[data-id=\'tooltip\']').clientHeight : 0;
var offset = { x: 0, y: 0 };
var effect = this.state.effect;

if (effect === 'float') {
if (this.state.place === 'top') {
offset.x = -(tipWidth / 2);
offset.y = -50;
} else if (this.state.place === 'bottom') {
offset.x = -(tipWidth / 2);
offset.y = 30;
} else if (this.state.place === 'left') {
offset.x = -(tipWidth + 15);
offset.y = -(tipHeight / 2);
} else if (this.state.place === 'right') {
offset.x = 10;
offset.y = -(tipHeight / 2);
}
}
var xPosition = 0;var yPosition = 0;var position = this.state.position;

if (Object.prototype.toString.apply(position) === '[object String]') {
position = JSON.parse(position);
}
for (var key in position) {
if (key === 'top') {
yPosition -= parseInt(position[key]);
} else if (key === 'bottom') {
yPosition += parseInt(position[key]);
} else if (key === 'left') {
xPosition -= parseInt(position[key]);
} else if (key === 'right') {
xPosition += parseInt(position[key]);
}
}

var node = _react2['default'].findDOMNode(this);

node.style.left = this.state.x + offset.x + xPosition + 'px';
node.style.top = this.state.y + offset.y + yPosition + 'px';
},

getInitialState: function getInitialState() {
return {
Expand All @@ -33,10 +78,14 @@ var ReactTooltip = _react2['default'].createClass({
place: '',
type: '',
effect: '',
position: {} };
position: {}
};
},

componentDidMount: function componentDidMount() {

this._updatePosition();

var targetArray = document.querySelectorAll('[data-tip]');
for (var i = 0; i < targetArray.length; i++) {
targetArray[i].addEventListener('mouseenter', this.showTooltip, false);
Expand All @@ -54,13 +103,18 @@ var ReactTooltip = _react2['default'].createClass({
}
},

componentDidUpdate: function componentDidUpdate() {
this._updatePosition();
},

showTooltip: function showTooltip(e) {
this.setState({
placeholder: e.target.getAttribute('data-tip'),
place: e.target.getAttribute('data-place') ? e.target.getAttribute('data-place') : this.props.place ? this.props.place : 'top',
type: e.target.getAttribute('data-type') ? e.target.getAttribute('data-type') : this.props.type ? this.props.type : 'dark',
effect: e.target.getAttribute('data-effect') ? e.target.getAttribute('data-effect') : this.props.effect ? this.props.effect : 'float',
position: e.target.getAttribute('data-position') ? e.target.getAttribute('data-position') : this.props.position ? this.props.position : {} });
position: e.target.getAttribute('data-position') ? e.target.getAttribute('data-position') : this.props.position ? this.props.position : {}
});
this.updateTooltip(e);
},

Expand Down Expand Up @@ -109,7 +163,8 @@ var ReactTooltip = _react2['default'].createClass({
this.setState({
show: false,
x: 'NONE',
y: 'NONE' });
y: 'NONE'
});
},

trim: function trim(string) {
Expand All @@ -134,53 +189,12 @@ var ReactTooltip = _react2['default'].createClass({
},

render: function render() {
var tipWidth = document.querySelector('[data-id=\'tooltip\']') ? document.querySelector('[data-id=\'tooltip\']').clientWidth : 0;
var tipHeight = document.querySelector('[data-id=\'tooltip\']') ? document.querySelector('[data-id=\'tooltip\']').clientHeight : 0;
var offset = { x: 0, y: 0 };
var effect = this.state.effect;

if (effect === 'float') {
if (this.state.place === 'top') {
offset.x = -(tipWidth / 2);
offset.y = -50;
} else if (this.state.place === 'bottom') {
offset.x = -(tipWidth / 2);
offset.y = 30;
} else if (this.state.place === 'left') {
offset.x = -(tipWidth + 15);
offset.y = -(tipHeight / 2);
} else if (this.state.place === 'right') {
offset.x = 10;
offset.y = -(tipHeight / 2);
}
}
var xPosition = 0;var yPosition = 0;var position = this.state.position;

if (Object.prototype.toString.apply(position) === '[object String]') {
position = JSON.parse(position);
}
for (var key in position) {
if (key === 'top') {
yPosition -= parseInt(position[key]);
} else if (key === 'bottom') {
yPosition += parseInt(position[key]);
} else if (key === 'left') {
xPosition -= parseInt(position[key]);
} else if (key === 'right') {
xPosition += parseInt(position[key]);
}
}

var style = {
left: this.state.x + offset.x + xPosition + 'px',
top: this.state.y + offset.y + yPosition + 'px'
};

var tooltipClass = (0, _classnames2['default'])('reactTooltip', { 'show': this.state.show }, { 'place-top': this.state.place === 'top' }, { 'place-bottom': this.state.place === 'bottom' }, { 'place-left': this.state.place === 'left' }, { 'place-right': this.state.place === 'right' }, { 'type-dark': this.state.type === 'dark' }, { 'type-success': this.state.type === 'success' }, { 'type-warning': this.state.type === 'warning' }, { 'type-error': this.state.type === 'error' }, { 'type-info': this.state.type === 'info' }, { 'type-light': this.state.type === 'light' });

return _react2['default'].createElement(
'span',
{ className: tooltipClass, style: style, 'data-id': 'tooltip' },
{ className: tooltipClass, 'data-id': 'tooltip' },
this.state.placeholder
);
}
Expand Down
105 changes: 58 additions & 47 deletions example/src/react-tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,55 @@ const ReactTooltip = React.createClass({
positon: React.PropTypes.object,
},

_updatePosition: function(){
let tipWidth = document.querySelector("[data-id='tooltip']")?document.querySelector("[data-id='tooltip']").clientWidth:0;
let tipHeight = document.querySelector("[data-id='tooltip']")?document.querySelector("[data-id='tooltip']").clientHeight:0;
let offset = {x:0, y:0};
let { effect } = this.state;
if(effect === "float") {
if(this.state.place === "top") {
offset.x = -(tipWidth/2);
offset.y = -50;
}
else if(this.state.place === "bottom") {
offset.x = -(tipWidth/2);
offset.y = 30;
}
else if(this.state.place === "left") {
offset.x = -(tipWidth + 15);
offset.y = -(tipHeight/2);
}
else if(this.state.place === "right") {
offset.x = 10;
offset.y = -(tipHeight/2);
}
}
let xPosition = 0, yPosition = 0, {position} = this.state;
if(Object.prototype.toString.apply(position) === "[object String]") {
position = JSON.parse(position);
}
for(let key in position) {
if(key === "top") {
yPosition -= parseInt(position[key]);
}
else if(key === "bottom") {
yPosition += parseInt(position[key]);
}
else if(key === "left") {
xPosition -= parseInt(position[key]);
}
else if(key === "right") {
xPosition += parseInt(position[key]);
}
}

let node = React.findDOMNode(this);

node.style.left = this.state.x + offset.x + xPosition + 'px';
node.style.top = this.state.y + offset.y + yPosition + 'px';

},

getInitialState() {
return {
show: false,
Expand All @@ -28,6 +77,9 @@ const ReactTooltip = React.createClass({
},

componentDidMount() {

this._updatePosition();

let targetArray = document.querySelectorAll("[data-tip]");
for(let i = 0; i < targetArray.length; i++) {
targetArray[i].addEventListener("mouseenter", this.showTooltip, false);
Expand All @@ -45,6 +97,10 @@ const ReactTooltip = React.createClass({
}
},

componentDidUpdate(){
this._updatePosition();
},

showTooltip(e) {
this.setState({
placeholder: e.target.getAttribute("data-tip"),
Expand Down Expand Up @@ -128,52 +184,7 @@ const ReactTooltip = React.createClass({
},

render() {
let tipWidth = document.querySelector("[data-id='tooltip']")?document.querySelector("[data-id='tooltip']").clientWidth:0;
let tipHeight = document.querySelector("[data-id='tooltip']")?document.querySelector("[data-id='tooltip']").clientHeight:0;
let offset = {x:0, y:0};
let { effect } = this.state;
if(effect === "float") {
if(this.state.place === "top") {
offset.x = -(tipWidth/2);
offset.y = -50;
}
else if(this.state.place === "bottom") {
offset.x = -(tipWidth/2);
offset.y = 30;
}
else if(this.state.place === "left") {
offset.x = -(tipWidth + 15);
offset.y = -(tipHeight/2);
}
else if(this.state.place === "right") {
offset.x = 10;
offset.y = -(tipHeight/2);
}
}
let xPosition = 0, yPosition = 0, {position} = this.state;
if(Object.prototype.toString.apply(position) === "[object String]") {
position = JSON.parse(position);
}
for(let key in position) {
if(key === "top") {
yPosition -= parseInt(position[key]);
}
else if(key === "bottom") {
yPosition += parseInt(position[key]);
}
else if(key === "left") {
xPosition -= parseInt(position[key]);
}
else if(key === "right") {
xPosition += parseInt(position[key]);
}
}

let style = {
left: this.state.x + offset.x + xPosition + "px",
top: this.state.y + offset.y + yPosition + "px"
}


let tooltipClass = classname(
'reactTooltip',
{"show": this.state.show},
Expand All @@ -190,7 +201,7 @@ const ReactTooltip = React.createClass({
);

return (
<span className={tooltipClass} style={style} data-id="tooltip">{this.state.placeholder}</span>
<span className={tooltipClass} data-id="tooltip">{this.state.placeholder}</span>
)
}
});
Expand Down
Loading