From 055f6534769a78d07c76a78d0e01bb7898f99444 Mon Sep 17 00:00:00 2001 From: Yiqing Zhu <793831308@qq.com> Date: Mon, 28 Mar 2022 21:02:55 +1100 Subject: [PATCH] Chap04 -> Creating a reusable styled component with Emotion --- QandA/frontend/src/HomePage.tsx | 16 ++++++++++++++-- QandA/frontend/src/Page.tsx | 11 ++++++++++- QandA/frontend/src/PageTitle.tsx | 17 ++++++++++++++++- QandA/frontend/src/Styles.ts | 23 +++++++++++++++++++++++ 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/QandA/frontend/src/HomePage.tsx b/QandA/frontend/src/HomePage.tsx index ba6c4fa..e95c91d 100644 --- a/QandA/frontend/src/HomePage.tsx +++ b/QandA/frontend/src/HomePage.tsx @@ -1,8 +1,12 @@ +/** @jsxImportSource @emotion/react */ +import { css } from '@emotion/react'; + import React from 'react'; import { PageTitle } from './PageTitle'; import { Page } from './Page'; import { QuestionList } from './QuestionList'; import { getUnansweredQuestions, QuestionData } from './QuestionsData'; +import { PrimaryButton } from './Styles'; export const HomePage = () => { const [questions, setQuestions] = React.useState([]); @@ -23,9 +27,17 @@ export const HomePage = () => { return ( -
+
Unanswered Questions - + + Ask a question +
{questionsLoading ? (
Loading...
diff --git a/QandA/frontend/src/Page.tsx b/QandA/frontend/src/Page.tsx index b9935b6..52e88dd 100644 --- a/QandA/frontend/src/Page.tsx +++ b/QandA/frontend/src/Page.tsx @@ -1,3 +1,6 @@ +/** @jsxImportSource @emotion/react */ +import { css } from '@emotion/react'; + import React from 'react'; import { PageTitle } from './PageTitle'; @@ -7,7 +10,13 @@ interface Props { } export const Page = ({ title, children }: Props) => ( -
+
{title && {title}} {children} {/* Component nested within Page component will be rendere here */} diff --git a/QandA/frontend/src/PageTitle.tsx b/QandA/frontend/src/PageTitle.tsx index a906d9e..f868fc6 100644 --- a/QandA/frontend/src/PageTitle.tsx +++ b/QandA/frontend/src/PageTitle.tsx @@ -1,5 +1,20 @@ +/** @jsxImportSource @emotion/react */ +import { css } from '@emotion/react'; + import React from 'react'; interface Props { children: React.ReactNode; } -export const PageTitle = ({ children }: Props) =>

{children}

; +export const PageTitle = ({ children }: Props) => ( +

+ {children} +

+); diff --git a/QandA/frontend/src/Styles.ts b/QandA/frontend/src/Styles.ts index 6266ae9..bf6cc3b 100644 --- a/QandA/frontend/src/Styles.ts +++ b/QandA/frontend/src/Styles.ts @@ -1,3 +1,5 @@ +import styled from '@emotion/styled'; + export const gray1 = '#383737'; export const gray2 = '#5c5a5a'; export const gray3 = '#857c81'; @@ -10,3 +12,24 @@ export const accent1 = '#dbb365'; export const accent2 = '#efd197'; export const fontFamily = "'Segoe UI', 'Helvetica Neue',sans-serif"; export const fontSize = '16px'; +export const PrimaryButton = styled.button` + background-color: ${primary2}; + border-color: ${primary2}; + border-style: solid; + border-radius: 5px; + font-family: ${fontFamily}; + font-family: ${fontSize}; + padding: 5px 10px; + color: white; + cursor: pointer; + :hover { + background-color: ${primary1}; + } + :focus { + outline-color: ${primary2}; + } + :disabled { + opacity: 0.5; + cursor: not-allowed; + } +`;