Peiyun Lee
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    ###### tags: `前端技能樹` # Todolist with React(中) >在上一章[Todolist with React (1)](),建立了專案環境、並且拆離 UI 設定好所有 Component 後,現在就讓我們使用套件 Styled-components 來為元素添加樣式。 <style> .markdown-body li { margin: 3px 0; list-style-type: disc; } </style> ![](https://i.imgur.com/IsQYUYU.png) >上面是目前的畫面 ![](https://i.imgur.com/iJQzRdn.png) >這是預計實現的樣式 ## App Component 首先是最外層的 App Component,要設定 - ```Wrapper```:包覆內部所有元素,設定背景和大小。 - ```Title```:Todolist 標題樣式 App.js ```jsx= import TaskList from "./TaskList"; import AddTask from "./AddTask"; function App() { return ( <Wrapper> <Title>Todolist</Title> <AddTask /> <TaskList /> </Wrapper> ); } ``` ```jsx= import styled from "styled-components"; const Wrapper = styled.div` background-color: #3c5d95; height: 100vh; padding-top: 70px; `; const Title = styled.h1` margin: 0; text-align: center; font-weight: bold; font-size: 28px; letter-spacing: 2px; color: #ffc236; text-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25); `; ``` ![](https://i.imgur.com/QLGm6gm.png) ### 清除 body margin / 設定字體 在 App.js 引入 CSS 檔 app.css ,清除 body 預設的 margin,並且設定網頁統一的字型樣式。 ```jsx= import "./app.css"; ``` app.css ```css= body { margin: 0; font-family: "Noto Sans TC", sans-serif; } ``` ![](https://i.imgur.com/Xti9FIh.png) ## AddTask Component 新增任務的區塊 AddTask Component,要設定: - ```Wrapper```:包覆內部所有元素,指定元素的排列規則。 - ```Input```:輸入框區塊,```&::placeholder``` 可以設定預設顯示文字的字體顏色。 - ```AddBtn```:新增按鈕區塊,利用巢狀寫法一定設定內部的圖片樣式。 AddTask.js ```jsx= import addIcon from "../assets/icon/add.png"; function AddTask() { return ( <Wrapper> <Input name="addtask" type="text" placeholder={"add new task ..."} /> <AddBtn> <img src={addIcon} alt=""/> </AddBtn> </Wrapper> ); } ``` ```jsx= import styled from "styled-components"; const Wrapper = styled.div` display: flex; justify-content: space-between; align-items: center; width: 300px; margin: 25px auto 40px; `; const Input = styled.input` padding: 0.5em; margin: 0.5em; background-color: inherit; border: none; border-bottom: 2px solid #333; width: 300px; font-size: 16px; letter-spacing: 0.05em; &::placeholder { color: #333; } `; const AddBtn = styled.button` padding: 0; border: none; height: 27px; width: 27px; background-color: inherit; img { cursor: pointer; } `; ``` ![](https://i.imgur.com/sCA5YwZ.png) ## TaskList Component 列出待辦事項,包含篩選器跟清單的 TaskList Component,要設定: - ```Wrapper```:設定寬度,控制內部元素的範圍。 - ```TaskItemContainer```:任務清單區塊,設定任務 TaskItem 的排列方式。 TaskList.js ```jsx= import Filter from "./Filter"; import TaskItem from "./TaskItem"; function TaskList() { return ( <Wrapper> <Filter /> <TaskItemContainer> <TaskItem /> <TaskItem /> <TaskItem /> </TaskItemContainer> </Wrapper> ); } ``` ```jsx= import styled from "styled-components"; const Wrapper = styled.div` margin: 0 auto; width: 80%; max-width: 600px; min-width: 300px; `; const TaskItemContainer = styled.div` display: flex; flex-direction: column; align-items: center; width: 100%; padding: 30px 0; background-color: #f8f8f8; `; ``` ![](https://i.imgur.com/bxMQKpa.png) ## Filter Component 再來是篩選器 Filter Component,要設定: - ```ButtonContainer```:設定內部按鈕的排列方式。 - ```Button```:指定按鈕樣式,```&:hover``` 可以設定滑鼠移至按鈕的樣式。 Filter.js ```jsx= function Filter() { return ( <ButtonContainer> <Button>ALL</Button> <Button>TODO</Button> <Button>DONE</Button> </ButtonContainer> ); } ``` ```jsx= import styled from "styled-components"; const ButtonContainer = styled.div` display: flex; `; const Button = styled.div` width: min-content; padding: 5px 10px; margin-right: 10px; background-color: #bebebe; border: none; border-bottom: 2px solid #3c5d95; border-radius: 3px 3px 0 0; color: white; letter-spacing: 0.1em; text-align: center; cursor: pointer; &:hover { background-color: #ffc236; } `; ``` ![](https://i.imgur.com/W2w5m79.png) ## TaskItem Components 代表每個任務的 TaskItem Component,要設定: - ```Wrapper```:設定整個任務方塊的大小和內部元素的排列方式 - ```CheckBox```:勾選框的樣式 - ```TaskName```:設定任務文字樣式 - ```Button```:刪除任務的按鈕樣式 TaskItem.js ```jsx= function TaskItem() { return ( <Wrapper> <CheckBox type="checkbox"/> <TaskName>NEW TASK</TaskName> <Button>Delete</Button> </Wrapper> ); } ``` ```jsx= import styled from "styled-components"; const Wrapper = styled.div` background-color: white; width: 80%; height: 60px; display: flex; justify-content: space-between; align-items: center; letter-spacing: 0.05em; padding: 0 20px; `; const CheckBox = styled.input.attrs({ type: 'checkbox' })` width: 18px; height: 18px; cursor: pointer; `; const TaskName = styled.div` flex-grow: 1; margin: 0 20px; `; const Button = styled.div` background-color: #bebebe; border: none; border-radius: 3px; padding: 5px 10px; width: min-content; color: white; letter-spacing: 0.05em; cursor: pointer; &:hover { background-color: #F5727E; } `; ``` ![](https://i.imgur.com/skLEki7.png) *** ## 小結 這樣就完成了所有的樣式設定,**建議大家在做的過程中,可以一段一段去觀察每個樣式會對網頁造成什麼樣的改變,進而去熟悉使用各式各樣的樣式**。在下一篇文章,就來在 React 中加入 Redux,使用 React-redux 動態產生任務清單,那我們就明天見囉! [範例程式碼](https://github.com/peiyunlee/ithome-21ironman-todolist) 如果文章中有錯誤的地方,要麻煩各位大大不吝賜教;喜歡的話,也要記得幫我按讚訂閱喔❤️

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully