Skip to content

Commit 7d4e186

Browse files
author
Mahesh Kothur
committed
feat(component): created utility for smoothScrolling
1 parent bd8d783 commit 7d4e186

File tree

5 files changed

+27
-35
lines changed

5 files changed

+27
-35
lines changed

packages/react/src/components/Cta/Cta.js

+4-34
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { FeaturedLink } from '../../patterns/blocks/FeaturedLink';
1111
import { LinkWithIcon } from '../LinkWithIcon';
1212
import PropTypes from 'prop-types';
1313
import React from 'react';
14+
import { smoothScrolling } from '@carbon/ibmdotcom-utilities';
1415

1516
/**
1617
* CTA component
@@ -28,26 +29,14 @@ const CTA = ({ style, type, ...cta }) => {
2829
<CardLink
2930
{...cta}
3031
icon={iconSelector(type)}
31-
onClick={
32-
type === 'jump'
33-
? e => {
34-
smoothScrolling(e, cta.id);
35-
}
36-
: null
37-
}
32+
onClick={type === 'jump' ? smoothScrolling : null}
3833
/>
3934
);
4035
case 'button':
4136
return (
4237
<ButtonGroup
4338
buttons={renderButtons(cta)}
44-
onClick={
45-
type === 'jump'
46-
? e => {
47-
smoothScrolling(e, cta.id);
48-
}
49-
: null
50-
}
39+
onClick={type === 'jump' ? smoothScrolling : null}
5140
/>
5241
);
5342
case 'feature':
@@ -57,33 +46,14 @@ const CTA = ({ style, type, ...cta }) => {
5746
<LinkWithIcon
5847
href={cta.href}
5948
target={type === 'external' ? '_blank' : null}
60-
onClick={
61-
type === 'jump'
62-
? e => {
63-
smoothScrolling(e, cta.id);
64-
}
65-
: null
66-
}>
49+
onClick={type === 'jump' ? smoothScrolling : null}>
6750
{cta.copy}
6851
{iconSelector(type)}
6952
</LinkWithIcon>
7053
);
7154
}
7255
};
7356

74-
/**
75-
* Handle OnClick
76-
*
77-
* @param {*} e event object
78-
* @param {*} id element id
79-
*/
80-
const smoothScrolling = (e, id) => {
81-
e.preventDefault();
82-
document.querySelector(`[id="${id}"]`).scrollIntoView({
83-
behavior: 'smooth',
84-
block: 'start',
85-
});
86-
};
8757
/**
8858
* TEMPORARY sets icon based on link type
8959
*

packages/react/src/components/Cta/__stories__/Cta.stories.js

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ storiesOf('Components|CTA', module)
3131
type: type,
3232
href: urlBy[type],
3333
copy: copy[0],
34-
id: 'example',
3534
};
3635
break;
3736
case 'card':

packages/utilities/src/utilities/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ export * from './markdownToHtml';
1414
export * from './sameHeight';
1515
export * from './serialize';
1616
export * from './settings';
17+
export * from './smoothScrolling';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright IBM Corp. 2016, 2018
3+
*
4+
* This source code is licensed under the Apache-2.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
export { default as smoothScrolling } from './smoothScrolling';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Handle OnClick
3+
*
4+
* @param {*} e event object
5+
*/
6+
const smoothScrolling = e => {
7+
e.preventDefault();
8+
const id = e.currentTarget.getAttribute('href');
9+
document.querySelector(id).scrollIntoView({
10+
behavior: 'smooth',
11+
block: 'start',
12+
});
13+
};
14+
export default smoothScrolling;

0 commit comments

Comments
 (0)