Skip to content

Commit

Permalink
fix: nested list styling for v5 (#1438)
Browse files Browse the repository at this point in the history
Co-authored-by: Eric Garcia <eng618@garciaericn.com>
  • Loading branch information
alisonjoseph and eng618 committed Mar 9, 2024
1 parent b8d4961 commit 37fb939
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 140 deletions.
4 changes: 2 additions & 2 deletions packages/example/src/pages/components/markdown.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ recommend using `h2` tags for section headings within your content.
- Or hyphens to create list items
- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et
- dolore magna aliqua. Pharetra massa massa ultricies mi quis. Adipiscing enim
eu turpis egestas pretium aenean.
- dolore magna _aliqua_. Pharetra **massa massa** ultricies mi quis. Adipiscing
enim eu turpis [egestas](#) pretium aenean.
- Massa eget egestas purus viverra accumsan in nisl nisi.
- Dolor sit amet consectetur adipiscing elit pellentesque habitant morbi
tristique.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ const components = {
ol: Ol,
ul: Ul,
li: Li,
'li.ul': (props) => <Ul nested {...props} />,
'li.ol': (props) => <Ol nested {...props} />,
blockquote: Blockquote,
code: Code,
table: PageTable,
Expand Down
25 changes: 17 additions & 8 deletions packages/gatsby-theme-carbon/src/components/markdown/Li.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import React from 'react';
import cx from 'classnames';
import { listItem } from './Markdown.module.scss';
import { ListItem } from '@carbon/react';

const Li = ({ children, className, ...rest }) => (
<li className={cx(className, `cds--list__item`, listItem)} {...rest}>
{children}
</li>
);
const { Provider, Consumer: LiConsumer } = React.createContext({
hasListItemParent: false,
});

export default Li;
const Li = ({ children, ...rest }) =>
React.createElement(
ListItem,
{ ...rest },
React.createElement(
Provider,
{ value: { hasListItemParent: true } },
children
)
);

// eslint-disable-next-line no-restricted-exports
export { Li as default, LiConsumer };
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,33 @@
}
}

// responsive items in a list need to be full width
.list-item .paragraph,
.list-item :global(.cds--row) {
width: 100%;
//---------------------------------------
// Lists OL and UL
//---------------------------------------

ol.ordered {
margin-left: $spacing-06;
width: calc(100% - $spacing-05);

@include breakpoint('md') {
width: calc(75% - $spacing-07);
}

@include breakpoint('lg') {
width: calc(66.667% - $spacing-07);
}
}

ul.unordered {
width: calc(100% - $spacing-05);

@include breakpoint('md') {
width: calc(75% - $spacing-07);
}

@include breakpoint('lg') {
width: calc(66.667% - $spacing-07);
}
}

//---------------------------------------
Expand Down Expand Up @@ -61,97 +84,6 @@
--space: 0;
}

.list {
padding-right: $spacing-05;
margin-right: 0;
margin-left: 0;

@include breakpoint('md') {
width: 100%;
padding-right: $spacing-07;
margin-right: 0;
margin-left: 0;
}

@include breakpoint('lg') {
width: 66.667%;
}
}

//spacing exception for nested list items
:global(.cds--list--nested) {
--space: 0;
width: 100%;
}

ul :global(.cds--list--ordered--native),
ul :global(.cds--list--unordered) {
width: 100%;
padding-right: $spacing-05;
}

:global(.cds--list--ordered--native) .list-item,
:global(.cds--list--unordered) .list-item {
@include type-style('body-long-02');
padding-right: 0;
width: 100%;
margin-left: $spacing-05;
padding-left: $spacing-02;
}

.list:not(:global(.cds--list--nested)) > .list-item {
padding-right: 2rem;
@include breakpoint('md') {
width: 75%;
}
@include breakpoint('lg') {
width: 100%;
}
}

// aligns UNnested ordered list item to body copy
:global(.cds--list--ordered--native:not(.cds--list--nested)) {
margin-left: 1ch;
}

// aligns NESTED ordered list item to parent list item text #993
:global(.cds--list--ordered--native.cds--list--nested) {
margin-left: 0.5rem;
}

//Safari only fixes to align unnested/nested ordered list to body copy #993
@media screen and (-webkit-min-device-pixel-ratio: 0) {
:global(.cds--list--ordered:not(.cds--list--nested)) {
padding-left: 0.5ch;
}

:global(.cds--list--ordered.cds--list--nested) {
padding-left: 0.25rem;
}
}

// Fix bleed when code snippets are used in a list
.list-item > pre {
margin-left: 1rem;
margin-top: $spacing-05;
@include breakpoint('md') {
margin-right: $spacing-05 * -1;
}
}

:global(.container--dark) :global(.cds--list__item) {
color: $white;
}

//spacing for content within list item
.list-item > * + *:not(:global(.cds--link)) {
margin-top: var(--space);

&:last-child {
margin-bottom: var(--space);
}
}

//---------------------------------------
// Blockquote
//---------------------------------------
Expand Down Expand Up @@ -197,16 +129,6 @@ div {
text-indent: 0;
}

:global(
.cds--list--ordered:not(.cds--list--nested) > .cds--list__item::before
) {
display: none;
}

:global(.cds--list--ordered:not(.cds--list--nested)) .list-item {
margin-left: 0.5rem;
}

// If paragraph is inside a user specified row then allow the grid code to set the width and padding
:global(.cds--row .cds--row) .paragraph {
width: 100%;
Expand Down
40 changes: 28 additions & 12 deletions packages/gatsby-theme-carbon/src/components/markdown/Ol.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import React from 'react';
import cx from 'classnames';
import { list } from './Markdown.module.scss';
import { OrderedList } from '@carbon/react';
import { LiConsumer } from './Li.js';
import { ordered } from './Markdown.module.scss';

const Ol = ({ children, nested, start, className, ...rest }) => {
const classNames = cx(className, `cds--list--ordered--native`, list, {
[`cds--list--nested`]: nested,
const Ol = ({ children, className, ...rest }) =>
React.createElement(LiConsumer, null, (value) => {
if (value.hasListItemParent) {
return React.createElement(
OrderedList,
{
isExpressive: true,
nested: true,
native: true,
...rest,
},
children
);
}
return React.createElement(
OrderedList,
{
isExpressive: true,
nested: false,
native: true,
className: ordered,
...rest,
},
children
);
});

return (
<ol className={classNames} {...rest} start={`${start}`}>
{children}
</ol>
);
};

export default Ol;
37 changes: 26 additions & 11 deletions packages/gatsby-theme-carbon/src/components/markdown/Ul.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import React from 'react';
import cx from 'classnames';
import { list } from './Markdown.module.scss';
import { UnorderedList } from '@carbon/react';
import { LiConsumer } from './Li.js';
import { unordered } from './Markdown.module.scss';

const Ul = ({ children, nested, className, ...rest }) => {
const classNames = cx(className, `cds--list--unordered`, list, {
[`cds--list--nested`]: nested,
const Ul = ({ children, className, ...rest }) =>
React.createElement(LiConsumer, null, (value) => {
if (value.hasListItemParent) {
return React.createElement(
UnorderedList,
{
isExpressive: true,
nested: true,
...rest,
},
children
);
}
return React.createElement(
UnorderedList,
{
isExpressive: true,
nested: false,
className: unordered,
...rest,
},
children
);
});
return (
<ul className={classNames} {...rest}>
{children}
</ul>
);
};

export default Ul;

0 comments on commit 37fb939

Please sign in to comment.