1
0
mirror of https://github.com/phuoc-ng/csslayout.git synced 2025-08-14 01:54:47 +02:00

feat: Questions and answers

This commit is contained in:
Phuoc Nguyen
2022-09-19 22:34:07 +07:00
parent c7dea4ee85
commit 505b85fb7e
8 changed files with 140 additions and 233 deletions

View File

@@ -50,7 +50,7 @@ module.exports = function(eleventyConfig) {
eleventyConfig.addShortcode('rectangle', function(dir, size, width) {
const direction = dir || 'hor';
const s = size || 'sm';
const w = width || randomInteger(1, 4) * 20;
const w = width || randomInteger(2, 4) * 20;
return `<div class="rectangle rectangle--${direction} rectangle--${s} rectangle--${w}"></div>`;
});
eleventyConfig.addShortcode('square', function(size) {

View File

@@ -0,0 +1,35 @@
<div class="questions-and-answers">
<div class="questions-and-answers__item questions-and-answers__item--collapsed">
<div class="questions-and-answers__header">
<div class="questions-and-answers__title">
{% rectangle %}
</div>
<div class="questions-and-answers__toggle">
{% triangle "r" %}
</div>
</div>
</div>
<div class="questions-and-answers__item questions-and-answers__item--expanded">
<div class="questions-and-answers__header">
<div class="questions-and-answers__title">
{% rectangle %}
</div>
<div class="questions-and-answers__toggle">
{% triangle "b" %}
</div>
</div>
<div class="questions-and-answers__content">
{% lines "hor", 3 %}
</div>
</div>
<div class="questions-and-answers__item questions-and-answers__item--collapsed">
<div class="questions-and-answers__header">
<div class="questions-and-answers__title">
{% rectangle %}
</div>
<div class="questions-and-answers__toggle">
{% triangle "r" %}
</div>
</div>
</div>
</div>

View File

@@ -218,9 +218,9 @@ eleventyExcludeFromCollections: true
</a>
</div>
<div class="pattern__item">
<a class="pattern__link" href="/triangle-buttons/">
<div class="pattern__cover">{% include "patterns/triangle-buttons.njk" %}</div>
<div class="pattern__title">Triangle buttons</div>
<a class="pattern__link" href="/questions-and-answers/">
<div class="pattern__cover">{% include "patterns/questions-and-answers.njk" %}</div>
<div class="pattern__title">Questions and answers</div>
</a>
</div>
<div class="pattern__item">

View File

@@ -0,0 +1,71 @@
---
layout: layouts/post.njk
title: Questions and answers
description: Create a questions and answers section with CSS flexbox
keywords: css accordion, css faq, css flexbox
---
## HTML
```html
<!-- Collapsed question and answer item -->
<div class="questions-and-answers__item questions-and-answers__item--collapsed">
<!-- Heading -->
<div class="questions-and-answers__header">
<!-- Question -->
<div class="questions-and-answers__title">
...
</div>
<!-- The toggle icon sticks to the right -->
<div class="questions-and-answers__toggle">
</div>
</div>
<!-- Answer -->
<div class="questions-and-answers__content">
...
</div>
</div>
<!-- Expanded question and answer item -->
<div class="questions-and-answers__item questions-and-answers__item--expanded">
...
</div>
```
## CSS
```css
.questions-and-answers__item:not(:last-child) {
border-bottom: 1px solid rgba(0, 0, 0, 0.3);
}
.questions-and-answers__header {
align-items: center;
display: flex;
justify-content: center;
cursor: pointer;
padding: 0.5rem;
}
.questions-and-answers__toggle {
margin-right: 0.25rem;
}
.questions-and-answers__title {
/* Take the available width */
flex: 1;
}
.questions-and-answers__content {
padding: 0 0.5rem;
}
.questions-and-answers__item--collapsed .questions-and-answers__content {
display: none;
}
.questions-and-answers__item--expanded .questions-and-answers__content {
display: block;
}
```
{% demo %}
{% include "patterns/questions-and-answers.njk" %}
{% enddemo %}

View File

@@ -1,161 +0,0 @@
import * as React from 'react';
import Head from 'next/head';
import { Spacer } from '@1milligram/design';
import { RelatedPatterns } from '../../components/RelatedPatterns';
import { Pattern } from '../../constants/Pattern';
import { PatternLayout } from '../../layouts/PatternLayout';
import Block from '../../placeholders/Block';
import BrowserFrame from '../../placeholders/BrowserFrame';
import Rectangle from '../../placeholders/Rectangle';
import Triangle from '../../placeholders/Triangle';
interface ItemProps {
index: number;
title: React.ReactNode;
}
const Details: React.FC<{}> = () => {
const [activeItem, setActiveItem] = React.useState(-1);
const Item: React.FC<ItemProps> = ({ index, title, children }) => {
const isOpened = index === activeItem;
const click = () => setActiveItem(isOpened ? -1 : index);
return (
<>
<div
style={{
alignItems: 'center',
cursor: 'pointer',
display: 'flex',
justifyContent: 'space-between',
padding: '16px 0',
}}
onClick={click}
>
{title}
<Triangle size={8} corner={isOpened ? 't' : 'b'} />
</div>
<div
style={{
display: isOpened ? 'block' : 'none',
marginBottom: '16px',
}}
>
{children}
</div>
</>
);
};
return (
<PatternLayout pattern={Pattern.QuestionsAndAnswers}>
<Head>
<meta name="description" content="Create a questions and answers section with CSS flexbox" />
<meta name="og:description" content="Create a questions and answers section with CSS flexbox" />
<meta name="twitter:description" content="Create a questions and answers section with CSS flexbox" />
<meta name="keywords" content="css accordion, css faq, css flexbox" />
</Head>
<BrowserFrame
html={`
<!-- Each question and answer item -->
<div class="container">
<!-- Heading -->
<div class="container__heading">
<!-- Question -->
...
<!-- The toggle icon sticks to the right -->
...
</div>
<!-- Answer -->
</div>
`}
css={`
.container {
border-bottom: 1px solid rgba(0, 0, 0, 0.3);
}
.container__heading {
display: flex;
align-items: center;
justify-content: space-between;
}
`}
>
<div
style={{
alignItems: 'center',
display: 'flex',
flexDirection: 'column',
height: '100%',
justifyContent: 'center',
padding: '8px',
}}
>
<div style={{ width: '60%' }}>
<div
style={{
margin: '0 auto',
paddingBottom: '24px',
width: '200px',
}}
>
<Rectangle />
</div>
<div
style={{
borderBottom: '1px solid rgba(0, 0, 0, 0.3)',
borderTop: '1px solid rgba(0, 0, 0, 0.3)',
}}
>
<Item
index={0}
title={
<div style={{ width: '40%' }}>
<Rectangle />
</div>
}
>
<Block numberOfBlocks={10} />
</Item>
</div>
<div
style={{
borderBottom: '1px solid rgba(0, 0, 0, 0.3)',
}}
>
<Item
index={1}
title={
<div style={{ width: '80%' }}>
<Rectangle />
</div>
}
>
<Block numberOfBlocks={15} />
</Item>
</div>
<div style={{ borderBottom: '1px solid rgba(0, 0, 0, 0.3)' }}>
<Item
index={2}
title={
<div style={{ width: '60%' }}>
<Rectangle />
</div>
}
>
<Block numberOfBlocks={10} />
</Item>
</div>
</div>
</div>
</BrowserFrame>
<Spacer size="extraLarge" />
<RelatedPatterns patterns={[Pattern.Accordion]} />
</PatternLayout>
);
};
export default Details;

View File

@@ -1,68 +0,0 @@
import * as React from 'react';
import Frame from '../../placeholders/Frame';
import Line from '../../placeholders/Line';
import Rectangle from '../../placeholders/Rectangle';
import Triangle from '../../placeholders/Triangle';
const Cover: React.FC<{}> = () => {
return (
<Frame>
<div
style={{
alignItems: 'center',
display: 'flex',
flexDirection: 'column',
height: '100%',
justifyContent: 'center',
padding: '8px',
}}
>
<div style={{ width: '80%' }}>
<div
style={{
alignItems: 'center',
display: 'flex',
justifyContent: 'space-between',
marginBottom: '8px',
}}
>
<div style={{ width: '60%' }}>
<Rectangle height={4} />
</div>
<Triangle size={6} corner="b" />
</div>
<div>
<div
style={{
alignItems: 'center',
display: 'flex',
justifyContent: 'space-between',
marginBottom: '4px',
}}
>
<div style={{ width: '40%' }}>
<Rectangle height={4} />
</div>
<Triangle size={6} corner="t" />
</div>
<div style={{ marginBottom: '4px', width: '100%' }}>
<Line />
</div>
<div style={{ marginBottom: '4px', width: '80%' }}>
<Line />
</div>
<div style={{ marginBottom: '4px', width: '60%' }}>
<Line />
</div>
<div style={{ width: '40%' }}>
<Line />
</div>
</div>
</div>
</div>
</Frame>
);
};
export default Cover;

View File

@@ -45,6 +45,7 @@
@import './patterns/price-tag';
@import './patterns/pricing-table';
@import './patterns/property-list';
@import './patterns/questions-and-answers';
@import './patterns/triangle-buttons';
@import './patterns/video-background';
@import './patterns/voting';

View File

@@ -0,0 +1,29 @@
.questions-and-answers {
width: 100%;
}
.questions-and-answers__item:not(:last-child) {
border-bottom: 1px solid rgba(0, 0, 0, 0.3);
}
.questions-and-answers__header {
align-items: center;
display: flex;
justify-content: center;
cursor: pointer;
padding: 0.5rem;
}
.questions-and-answers__toggle {
margin-right: 0.25rem;
}
.questions-and-answers__title {
flex: 1;
}
.questions-and-answers__content {
padding: 0 0.5rem;
}
.questions-and-answers__item--collapsed .questions-and-answers__content {
display: none;
}
.questions-and-answers__item--expanded .questions-and-answers__content {
display: block;
}