Skip to content

Support the @Row directive #409

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

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
12 changes: 12 additions & 0 deletions src/components/ContentNode.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import Table from './ContentNode/Table.vue';
import StrikeThrough from './ContentNode/StrikeThrough.vue';
import Small from './ContentNode/Small.vue';
import BlockVideo from './ContentNode/BlockVideo.vue';
import Column from './ContentNode/Column.vue';
import Row from './ContentNode/Row.vue';

const BlockType = {
aside: 'aside',
Expand All @@ -37,6 +39,7 @@ const BlockType = {
dictionaryExample: 'dictionaryExample',
small: 'small',
video: 'video',
row: 'row',
};

const InlineType = {
Expand Down Expand Up @@ -299,6 +302,15 @@ function renderNode(createElement, references) {
})
) : null;
}
case BlockType.row: {
return createElement(
Row, { props: { columns: node.numberOfColumns } }, node.columns.map(col => (
createElement(
Column, { props: { span: col.size } }, renderChildren(col.content),
)
)),
);
}
case InlineType.codeVoice:
return createElement(CodeVoice, {}, (
node.code
Expand Down
41 changes: 41 additions & 0 deletions src/components/ContentNode/Column.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!--
This source file is part of the Swift.org open source project

Copyright (c) 2022 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
-->
<template>
<div class="column" :style="style">
<slot />
</div>
</template>

<script>
export default {
name: 'Column',
props: {
span: {
type: Number,
default: null,
},
},
computed: {
style: ({ span }) => ({ '--col-span': span }),
},
};
</script>

<style scoped lang='scss'>
@import 'docc-render/styles/_core.scss';

.column {
grid-column: span var(--col-span);
min-width: 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is min-width: 0; needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tells the flex/column item to become smaller than it can right now, so it does not overflow.

If we have images or text that are wider than the column, this will make the column outgrow what it should.

@include breakpoint(small) {
grid-column: span 1;
}
}
</style>
72 changes: 72 additions & 0 deletions src/components/ContentNode/Row.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<!--
This source file is part of the Swift.org open source project

Copyright (c) 2022 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
-->
<template>
<div class="row" :style="style" :class="{ 'with-columns': columns }">
<slot />
</div>
</template>

<script>
/**
* A Row component that accepts an optional `columns` prop.
* When columns is passed, the grid will have that exact number of columns.
* If no `columns` provided, width is split up equally across each cell.
*/
export default {
name: 'Row',
props: {
columns: {
type: Number,
default: null,
required: false,
validator: v => v > 0,
},
gap: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we using this prop at the moment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not right now, but I am assuming we may start to, if we start using this as a building block for future components.

type: Number,
required: false,
},
},
computed: {
style: ({ columns, gap }) => ({
'--col-count': columns,
'--col-gap': gap && `${gap}px`,
}),
},
};
</script>

<style scoped lang='scss'>
@import 'docc-render/styles/_core.scss';

.row {
display: grid;
grid-auto-flow: column;
grid-auto-columns: 1fr;
grid-gap: var(--col-gap, #{$article-stacked-margin-small});

&.with-columns {
grid-template-columns: repeat(var(--col-count), 1fr);
grid-auto-flow: row;

@include breakpoint(small) {
grid-template-columns: 1fr;
}
}

@include breakpoint(small) {
grid-template-columns: 1fr;
grid-auto-flow: row;
}

/deep/ + * {
margin-top: $stacked-margin-large;
}
}
</style>
53 changes: 52 additions & 1 deletion tests/unit/components/ContentNode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import LinkableHeading from 'docc-render/components/ContentNode/LinkableHeading.
import StrikeThrough from 'docc-render/components/ContentNode/StrikeThrough.vue';
import Small from '@/components/ContentNode/Small.vue';
import BlockVideo from '@/components/ContentNode/BlockVideo.vue';
import Row from '@/components/ContentNode/Row.vue';
import Column from '@/components/ContentNode/Column.vue';

const { TableHeaderStyle } = ContentNode.constants;

Expand Down Expand Up @@ -342,6 +344,54 @@ describe('ContentNode', () => {
});
});

describe('with type="row"', () => {
it('renders a `<Row>` and `<Column>`', () => {
const wrapper = mountWithItem({
type: 'row',
numberOfColumns: 4,
columns: [
{
size: 2,
content: [
{
type: 'paragraph',
inlineContent: [
{
type: 'text',
text: 'foo',
},
],
},
],
},
{
content: [
{
type: 'paragraph',
inlineContent: [
{
type: 'text',
text: 'bar',
},
],
},
],
},
],
});
const grid = wrapper.find(Row);
expect(grid.props()).toEqual({
columns: 4,
});
const columns = grid.findAll(Column);
expect(columns).toHaveLength(2);
expect(columns.at(0).props()).toEqual({ span: 2 });
expect(columns.at(0).find('p').text()).toBe('foo');
expect(columns.at(1).props()).toEqual({ span: null });
expect(columns.at(1).find('p').text()).toBe('bar');
});
});

describe('with type="codeVoice"', () => {
it('renders a `CodeVoice`', () => {
const wrapper = mountWithItem({
Expand Down Expand Up @@ -1227,7 +1277,8 @@ describe('ContentNode', () => {

const content = wrapper.find(StrikeThrough);
// assert the `strong` tag is rendered
expect(content.html()).toBe('<strikethrough-stub>2<strong>strong</strong></strikethrough-stub>');
expect(content.html())
.toBe('<strikethrough-stub>2<strong>strong</strong></strikethrough-stub>');
});
});

Expand Down
32 changes: 32 additions & 0 deletions tests/unit/components/ContentNode/Column.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* This source file is part of the Swift.org open source project
*
* Copyright (c) 2022 Apple Inc. and the Swift project authors
* Licensed under Apache License v2.0 with Runtime Library Exception
*
* See https://swift.org/LICENSE.txt for license information
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import Column from '@/components/ContentNode/Column.vue';
import { shallowMount } from '@vue/test-utils';

const createWrapper = props => shallowMount(Column, {
slots: {
default: 'Default Content',
},
...props,
});

describe('Column', () => {
it('renders the Column', () => {
const wrapper = createWrapper();
expect(wrapper.classes()).toContain('column');
expect(wrapper.text()).toBe('Default Content');
expect(wrapper.vm.style).toHaveProperty('--col-span', null);
wrapper.setProps({
span: 5,
});
expect(wrapper.vm.style).toHaveProperty('--col-span', 5);
});
});
47 changes: 47 additions & 0 deletions tests/unit/components/ContentNode/Row.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* This source file is part of the Swift.org open source project
*
* Copyright (c) 2022 Apple Inc. and the Swift project authors
* Licensed under Apache License v2.0 with Runtime Library Exception
*
* See https://swift.org/LICENSE.txt for license information
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import Row from '@/components/ContentNode/Row.vue';
import { shallowMount } from '@vue/test-utils';

const createWrapper = props => shallowMount(Row, {
slots: {
default: 'Slot Content',
},
...props,
});

describe('Row', () => {
it('renders the Row', () => {
const wrapper = createWrapper();
expect(wrapper.classes()).toContain('row');
expect(wrapper.classes()).not.toContain('with-columns');
expect(wrapper.text()).toContain('Slot Content');
});

it('renders with columns in mind', () => {
const wrapper = createWrapper({
propsData: {
columns: 4,
},
});
expect(wrapper.classes()).toContain('with-columns');
expect(wrapper.vm.style).toHaveProperty('--col-count', 4);
});

it('provides a --col-gap', () => {
const wrapper = createWrapper({
propsData: {
gap: 10,
},
});
expect(wrapper.vm.style).toHaveProperty('--col-gap', '10px');
});
});