Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ndla/carousel

Package Overview
Dependencies
Maintainers
5
Versions
190
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ndla/carousel - npm Package Compare versions

Comparing version 0.2.0 to 0.2.1

es/CarouselAutosize.js

144

es/Carousel.js

@@ -5,4 +5,2 @@ import { jsx as ___EmotionJSX } from "@emotion/core";

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

@@ -36,8 +34,4 @@

import { ChevronRight, ChevronLeft } from '@ndla/icons/common';
import BEMHelper from 'react-bem-helper';
import { Swipe } from './Swipe';
var classes = new BEMHelper({
name: 'film-movielist',
prefix: 'c-'
});
import { Swipeable } from 'react-swipeable';
import { slideWrapperCSS, StyledButton, StyledSlideContent } from './Styles';
export var Carousel =

@@ -63,4 +57,6 @@ /*#__PURE__*/

_defineProperty(_assertThisInitialized(_this), "slideshow", React.createRef());
_defineProperty(_assertThisInitialized(_this), "swipeSpeed", 0);
_defineProperty(_assertThisInitialized(_this), "slideshowRef", React.createRef());
_defineProperty(_assertThisInitialized(_this), "state", {

@@ -73,7 +69,13 @@ slideIndex: 0,

var _this$props = _this.props,
children = _this$props.children,
columnsPrSlide = _this$props.columnsPrSlide,
columnWidth = _this$props.columnWidth,
distanceBetweenItems = _this$props.distanceBetweenItems;
var moved = Math.round(_this.swipeDistance / (columnWidth + distanceBetweenItems));
distanceBetweenItems = _this$props.distanceBetweenItems,
items = _this$props.items;
if (!_this.state.swiping) {
return;
}
var roundedColumnsPrSlide = Math.floor(columnsPrSlide);
var moved = Math.round((-Math.min(_this.props.columnWidth / 2, _this.swipeSpeed * 5) + _this.swipeDistance) / (columnWidth + distanceBetweenItems));
_this.swipeDistance = 0;

@@ -87,4 +89,4 @@

slideIndex = 0;
} else if (slideIndex < -(children.length - columnsPrSlide)) {
slideIndex = -(children.length - columnsPrSlide);
} else if (slideIndex < -(items.length - columnsPrSlide)) {
slideIndex = -(items.length - roundedColumnsPrSlide);
}

@@ -104,3 +106,9 @@

_defineProperty(_assertThisInitialized(_this), "onSwipe", function (p) {
_defineProperty(_assertThisInitialized(_this), "onSwipe", function (eventData) {
var moved = -eventData.deltaX;
if (Math.abs(moved) < 15 || eventData.dir === 'Up' || eventData.dir === 'Down') {
return;
}
_this.setState({

@@ -110,6 +118,8 @@ swiping: true

_this.swipeDistance = p.x;
_this.swipeSpeed = _this.swipeDistance - moved;
_this.swipeDistance = moved;
var transformX = _this.swipeDistance + _this.state.slideIndex * _this.props.columnWidth;
if (_this.slideshow.current) {
_this.slideshow.current.style.transform = "translateX(".concat(_this.swipeDistance + _this.state.slideIndex * (_this.props.columnWidth + _this.props.distanceBetweenItems), "px)");
if (_this.slideshowRef.current) {
_this.slideshowRef.current.style.transform = "translateX(".concat(transformX, "px)");
}

@@ -120,13 +130,14 @@ });

var _this$props2 = _this.props,
children = _this$props2.children,
columnsPrSlide = _this$props2.columnsPrSlide;
columnsPrSlide = _this$props2.columnsPrSlide,
items = _this$props2.items;
var roundedColumnsPrSlide = Math.floor(columnsPrSlide);
if (columnsPrSlide < children.length) {
if (roundedColumnsPrSlide < items.length) {
_this.setState(function (prevState) {
var slideIndex = prevState.slideIndex + columnsPrSlide * direction;
var slideIndex = prevState.slideIndex + roundedColumnsPrSlide * direction;
if (slideIndex > 0) {
slideIndex = 0;
} else if (slideIndex < -(children.length - columnsPrSlide)) {
slideIndex = -(children.length - columnsPrSlide);
} else if (slideIndex < -(items.length - roundedColumnsPrSlide)) {
slideIndex = -(items.length - roundedColumnsPrSlide);
}

@@ -150,54 +161,72 @@

var _this$props3 = this.props,
children = _this$props3.children,
items = _this$props3.items,
columnWidth = _this$props3.columnWidth,
columnsPrSlide = _this$props3.columnsPrSlide,
margin = _this$props3.margin,
distanceBetweenItems = _this$props3.distanceBetweenItems,
margin = _this$props3.margin,
slideBackwardsLabel = _this$props3.slideBackwardsLabel,
slideForwardsLabel = _this$props3.slideForwardsLabel;
slideForwardsLabel = _this$props3.slideForwardsLabel,
arrowOffset = _this$props3.arrowOffset,
buttonClass = _this$props3.buttonClass,
wrapperClass = _this$props3.wrapperClass,
disableScroll = _this$props3.disableScroll;
var _this$state = this.state,
slideIndex = _this$state.slideIndex,
swiping = _this$state.swiping;
var numberOfSlides = children.length;
var hideButtons = columnsPrSlide >= numberOfSlides;
var marginString = "".concat(margin, "px");
return ___EmotionJSX("section", null, ___EmotionJSX(Swipe, {
nodeName: "div",
onSwipeEnd: this.onSwipeEnd,
onSwipe: this.onSwipe
}, ___EmotionJSX("div", classes('slide-wrapper'), ___EmotionJSX("button", _extends({
var hideButtons = columnsPrSlide >= items.length;
var transformX = this.swipeDistance + slideIndex * (columnWidth + distanceBetweenItems);
return ___EmotionJSX("section", null, ___EmotionJSX(Swipeable, {
onSwiped: this.onSwipeEnd,
onSwiping: this.onSwipe
}, ___EmotionJSX("div", {
css: slideWrapperCSS,
className: wrapperClass
}, !disableScroll && ___EmotionJSX(React.Fragment, null, ___EmotionJSX(StyledButton, {
type: "button",
"aria-label": slideBackwardsLabel,
style: {
width: marginString
}
}, classes('slide-navigation', {
className: buttonClass,
prev: true,
hidden: slideIndex === 0 || hideButtons
}), {
arrowOffset: arrowOffset,
dontShow: slideIndex === 0 || hideButtons,
onClick: function onClick() {
return _this2.slidePage(1);
}
}), ___EmotionJSX(ChevronLeft, null)), ___EmotionJSX("button", _extends({
}, ___EmotionJSX(ChevronLeft, null)), ___EmotionJSX(StyledButton, {
type: "button",
"aria-label": slideForwardsLabel,
style: {
width: marginString
}
}, classes('slide-navigation', {
className: buttonClass,
dontShow: hideButtons || Math.floor(columnsPrSlide) === items.length + slideIndex,
next: true,
hidden: hideButtons || slideIndex === -(numberOfSlides - columnsPrSlide)
}), {
arrowOffset: arrowOffset,
onClick: function onClick() {
return _this2.slidePage(-1);
}
}), ___EmotionJSX(ChevronRight, null)), ___EmotionJSX("div", _extends({}, classes('slide-content', swiping ? 'swiping' : ''), {
ref: this.slideshow,
}, ___EmotionJSX(ChevronRight, null))), ___EmotionJSX(StyledSlideContent, {
swiping: swiping,
ref: this.slideshowRef,
style: {
padding: "0 ".concat(marginString),
width: "".concat(numberOfSlides * (columnWidth + distanceBetweenItems) + margin * 2, "px"),
transform: "translateX(".concat(this.swipeDistance + slideIndex * (columnWidth + distanceBetweenItems), "px)")
padding: "0 ".concat(margin, "px"),
width: "".concat(items.length * columnWidth + distanceBetweenItems * (items.length - 1) + margin * 2, "px"),
transform: "translateX(".concat(transformX, "px)")
}
}), children))));
}, items.map(function (item) {
return item;
})))));
}
}], [{
key: "getDerivedStateFromProps",
value: function getDerivedStateFromProps(nextProps, prevState) {
var columnsPrSlide = nextProps.columnsPrSlide,
items = nextProps.items;
var slideIndex = prevState.slideIndex; // Check if resize caused carousel to be scrolled to far.
if (Math.floor(columnsPrSlide) - items.length > slideIndex) {
var adjustedSlideIndex = slideIndex + (Math.floor(columnsPrSlide) - items.length - slideIndex);
return {
slideIndex: adjustedSlideIndex
};
}
return null;
}
}]);

@@ -209,3 +238,8 @@

_defineProperty(Carousel, "defaultProps", {
margin: 0
items: [],
margin: 0,
distanceBetweenItems: 0,
arrowOffset: 0,
buttonClass: '',
wrapperClass: ''
});

@@ -0,2 +1,9 @@

/**
* Copyright (c) 2019-present, NDLA.
*
* This source code is licensed under the GPLv3 license found in the
* LICENSE file in the root directory of this source tree.
*
*/
export { Carousel } from './Carousel';
export { Swipe } from './Swipe';
export { CarouselAutosize } from './CarouselAutosize';

@@ -9,15 +9,17 @@ /**

import React, { Component } from 'react';
import { Position } from './Swipe';
interface Child {
id: string;
children: React.ReactNode;
}
interface Props {
children: Child[];
import { EventData } from 'react-swipeable';
export interface CalculatedProps {
columnsPrSlide: number;
columnWidth: number;
distanceBetweenItems: number;
arrowOffset: number;
margin: number;
}
interface Props extends CalculatedProps {
items: React.ReactNode[];
slideBackwardsLabel: string;
slideForwardsLabel: string;
margin: number;
buttonClass: string;
wrapperClass: string;
disableScroll: boolean;
}

@@ -30,9 +32,18 @@ interface State {

static defaultProps: {
items: never[];
margin: number;
distanceBetweenItems: number;
arrowOffset: number;
buttonClass: string;
wrapperClass: string;
};
swipeDistance: number;
slideshow: React.RefObject<HTMLDivElement>;
swipeSpeed: number;
slideshowRef: React.RefObject<HTMLDivElement>;
state: State;
static getDerivedStateFromProps(nextProps: Props, prevState: State): {
slideIndex: number;
} | null;
onSwipeEnd: () => void;
onSwipe: (p: Position) => void;
onSwipe: (eventData: EventData<HTMLElement>) => void;
slidePage: (direction: number) => void;

@@ -39,0 +50,0 @@ render(): JSX.Element;

@@ -14,8 +14,6 @@ "use strict";

var _reactBemHelper = _interopRequireDefault(require("react-bem-helper"));
var _reactSwipeable = require("react-swipeable");
var _Swipe = require("./Swipe");
var _Styles = require("./Styles");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

@@ -25,4 +23,2 @@

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

@@ -46,7 +42,2 @@

var classes = new _reactBemHelper.default({
name: 'film-movielist',
prefix: 'c-'
});
var Carousel =

@@ -72,4 +63,6 @@ /*#__PURE__*/

_defineProperty(_assertThisInitialized(_this), "slideshow", _react.default.createRef());
_defineProperty(_assertThisInitialized(_this), "swipeSpeed", 0);
_defineProperty(_assertThisInitialized(_this), "slideshowRef", _react.default.createRef());
_defineProperty(_assertThisInitialized(_this), "state", {

@@ -82,7 +75,13 @@ slideIndex: 0,

var _this$props = _this.props,
children = _this$props.children,
columnsPrSlide = _this$props.columnsPrSlide,
columnWidth = _this$props.columnWidth,
distanceBetweenItems = _this$props.distanceBetweenItems;
var moved = Math.round(_this.swipeDistance / (columnWidth + distanceBetweenItems));
distanceBetweenItems = _this$props.distanceBetweenItems,
items = _this$props.items;
if (!_this.state.swiping) {
return;
}
var roundedColumnsPrSlide = Math.floor(columnsPrSlide);
var moved = Math.round((-Math.min(_this.props.columnWidth / 2, _this.swipeSpeed * 5) + _this.swipeDistance) / (columnWidth + distanceBetweenItems));
_this.swipeDistance = 0;

@@ -96,4 +95,4 @@

slideIndex = 0;
} else if (slideIndex < -(children.length - columnsPrSlide)) {
slideIndex = -(children.length - columnsPrSlide);
} else if (slideIndex < -(items.length - columnsPrSlide)) {
slideIndex = -(items.length - roundedColumnsPrSlide);
}

@@ -113,3 +112,9 @@

_defineProperty(_assertThisInitialized(_this), "onSwipe", function (p) {
_defineProperty(_assertThisInitialized(_this), "onSwipe", function (eventData) {
var moved = -eventData.deltaX;
if (Math.abs(moved) < 15 || eventData.dir === 'Up' || eventData.dir === 'Down') {
return;
}
_this.setState({

@@ -119,6 +124,8 @@ swiping: true

_this.swipeDistance = p.x;
_this.swipeSpeed = _this.swipeDistance - moved;
_this.swipeDistance = moved;
var transformX = _this.swipeDistance + _this.state.slideIndex * _this.props.columnWidth;
if (_this.slideshow.current) {
_this.slideshow.current.style.transform = "translateX(".concat(_this.swipeDistance + _this.state.slideIndex * (_this.props.columnWidth + _this.props.distanceBetweenItems), "px)");
if (_this.slideshowRef.current) {
_this.slideshowRef.current.style.transform = "translateX(".concat(transformX, "px)");
}

@@ -129,13 +136,14 @@ });

var _this$props2 = _this.props,
children = _this$props2.children,
columnsPrSlide = _this$props2.columnsPrSlide;
columnsPrSlide = _this$props2.columnsPrSlide,
items = _this$props2.items;
var roundedColumnsPrSlide = Math.floor(columnsPrSlide);
if (columnsPrSlide < children.length) {
if (roundedColumnsPrSlide < items.length) {
_this.setState(function (prevState) {
var slideIndex = prevState.slideIndex + columnsPrSlide * direction;
var slideIndex = prevState.slideIndex + roundedColumnsPrSlide * direction;
if (slideIndex > 0) {
slideIndex = 0;
} else if (slideIndex < -(children.length - columnsPrSlide)) {
slideIndex = -(children.length - columnsPrSlide);
} else if (slideIndex < -(items.length - roundedColumnsPrSlide)) {
slideIndex = -(items.length - roundedColumnsPrSlide);
}

@@ -159,54 +167,72 @@

var _this$props3 = this.props,
children = _this$props3.children,
items = _this$props3.items,
columnWidth = _this$props3.columnWidth,
columnsPrSlide = _this$props3.columnsPrSlide,
margin = _this$props3.margin,
distanceBetweenItems = _this$props3.distanceBetweenItems,
margin = _this$props3.margin,
slideBackwardsLabel = _this$props3.slideBackwardsLabel,
slideForwardsLabel = _this$props3.slideForwardsLabel;
slideForwardsLabel = _this$props3.slideForwardsLabel,
arrowOffset = _this$props3.arrowOffset,
buttonClass = _this$props3.buttonClass,
wrapperClass = _this$props3.wrapperClass,
disableScroll = _this$props3.disableScroll;
var _this$state = this.state,
slideIndex = _this$state.slideIndex,
swiping = _this$state.swiping;
var numberOfSlides = children.length;
var hideButtons = columnsPrSlide >= numberOfSlides;
var marginString = "".concat(margin, "px");
return (0, _core.jsx)("section", null, (0, _core.jsx)(_Swipe.Swipe, {
nodeName: "div",
onSwipeEnd: this.onSwipeEnd,
onSwipe: this.onSwipe
}, (0, _core.jsx)("div", classes('slide-wrapper'), (0, _core.jsx)("button", _extends({
var hideButtons = columnsPrSlide >= items.length;
var transformX = this.swipeDistance + slideIndex * (columnWidth + distanceBetweenItems);
return (0, _core.jsx)("section", null, (0, _core.jsx)(_reactSwipeable.Swipeable, {
onSwiped: this.onSwipeEnd,
onSwiping: this.onSwipe
}, (0, _core.jsx)("div", {
css: _Styles.slideWrapperCSS,
className: wrapperClass
}, !disableScroll && (0, _core.jsx)(_react.default.Fragment, null, (0, _core.jsx)(_Styles.StyledButton, {
type: "button",
"aria-label": slideBackwardsLabel,
style: {
width: marginString
}
}, classes('slide-navigation', {
className: buttonClass,
prev: true,
hidden: slideIndex === 0 || hideButtons
}), {
arrowOffset: arrowOffset,
dontShow: slideIndex === 0 || hideButtons,
onClick: function onClick() {
return _this2.slidePage(1);
}
}), (0, _core.jsx)(_common.ChevronLeft, null)), (0, _core.jsx)("button", _extends({
}, (0, _core.jsx)(_common.ChevronLeft, null)), (0, _core.jsx)(_Styles.StyledButton, {
type: "button",
"aria-label": slideForwardsLabel,
style: {
width: marginString
}
}, classes('slide-navigation', {
className: buttonClass,
dontShow: hideButtons || Math.floor(columnsPrSlide) === items.length + slideIndex,
next: true,
hidden: hideButtons || slideIndex === -(numberOfSlides - columnsPrSlide)
}), {
arrowOffset: arrowOffset,
onClick: function onClick() {
return _this2.slidePage(-1);
}
}), (0, _core.jsx)(_common.ChevronRight, null)), (0, _core.jsx)("div", _extends({}, classes('slide-content', swiping ? 'swiping' : ''), {
ref: this.slideshow,
}, (0, _core.jsx)(_common.ChevronRight, null))), (0, _core.jsx)(_Styles.StyledSlideContent, {
swiping: swiping,
ref: this.slideshowRef,
style: {
padding: "0 ".concat(marginString),
width: "".concat(numberOfSlides * (columnWidth + distanceBetweenItems) + margin * 2, "px"),
transform: "translateX(".concat(this.swipeDistance + slideIndex * (columnWidth + distanceBetweenItems), "px)")
padding: "0 ".concat(margin, "px"),
width: "".concat(items.length * columnWidth + distanceBetweenItems * (items.length - 1) + margin * 2, "px"),
transform: "translateX(".concat(transformX, "px)")
}
}), children))));
}, items.map(function (item) {
return item;
})))));
}
}], [{
key: "getDerivedStateFromProps",
value: function getDerivedStateFromProps(nextProps, prevState) {
var columnsPrSlide = nextProps.columnsPrSlide,
items = nextProps.items;
var slideIndex = prevState.slideIndex; // Check if resize caused carousel to be scrolled to far.
if (Math.floor(columnsPrSlide) - items.length > slideIndex) {
var adjustedSlideIndex = slideIndex + (Math.floor(columnsPrSlide) - items.length - slideIndex);
return {
slideIndex: adjustedSlideIndex
};
}
return null;
}
}]);

@@ -220,3 +246,8 @@

_defineProperty(Carousel, "defaultProps", {
margin: 0
items: [],
margin: 0,
distanceBetweenItems: 0,
arrowOffset: 0,
buttonClass: '',
wrapperClass: ''
});

@@ -0,2 +1,9 @@

/**
* Copyright (c) 2019-present, NDLA.
*
* This source code is licensed under the GPLv3 license found in the
* LICENSE file in the root directory of this source tree.
*
*/
export { Carousel } from './Carousel';
export { Swipe } from './Swipe';
export { CarouselAutosize } from './CarouselAutosize';

@@ -12,6 +12,6 @@ "use strict";

});
Object.defineProperty(exports, "Swipe", {
Object.defineProperty(exports, "CarouselAutosize", {
enumerable: true,
get: function get() {
return _Swipe.Swipe;
return _CarouselAutosize.CarouselAutosize;
}

@@ -22,2 +22,2 @@ });

var _Swipe = require("./Swipe");
var _CarouselAutosize = require("./CarouselAutosize");
{
"name": "@ndla/carousel",
"version": "0.2.0",
"version": "0.2.1",
"description": "Carousel for NDLA",

@@ -28,3 +28,4 @@ "license": "GPL-3.0",

"dependencies": {
"@ndla/core": "^0.6.0"
"@ndla/core": "^0.6.1",
"react-swipeable": "^5.0.1"
},

@@ -40,3 +41,3 @@ "peerDependencies": {

},
"gitHead": "ceaf1645348ee539d9289af19c4042aa6613c633"
"gitHead": "84b8002661e3569d84ef1d57fc5e57cf2fe6f062"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc